Run a Code Block on Multiple Objects in Array Simultaneously

Hi all,

I need some guidance please. What it ultimately does is to convert Azure Managed Disks from Standard Storage to Premium Storage by importing a list of servers and running the operation against it. What the script does is irrelevant to what I’m trying to find out though.

The script works fine, but it is very slow, seeing as it runs through the entire script, and then goes to the next server and runs through the script, then the next and so on.

So, what am I asking?

How can I get this to execute the code blocks against each server in the array in parallel rather than one at a time?

Below is a copy of the script as it currently is.

Much appreciated.

$servers = Import-Csv ".\servers.csv"

foreach($item in $servers)
{
	$rgName = $($item.rgName)
	$vmName = $($item.vmName)
	$storageType = $($item.storageType)
 	$size = $($item.size)

 	Stop-AzurermVM -ResourceGroupName $rgName -Name $vmName -Force

 	$vm = Get-AzurermVM -Name $vmName -resourceGroupName $rgName

 	$vm.HardwareProfile.VmSize = $size
 	Update-AzurermVM -VM $vm -ResourceGroupName $rgName

 	$vmDisks = Get-AzurermDisk -ResourceGroupName $rgName 

foreach ($disk in $vmDisks)
{
 
	if ($disk.ManagedBy -eq $vm.Id)
 	{
  		$diskUpdateConfig = New-AzurermDiskUpdateConfig -AccountType $storageType
  		Update-AzurermDisk -DiskUpdate $diskUpdateConfig -ResourceGroupName $rgName -DiskName $disk.Name
 	}
}

Start-AzurermVM -ResourceGroupName $rgName -Name $vmName

}