I am trying to generate a 1000 Azure VMs(yes I know of the cost) for a large job. This is being done in a PowerShell script (see script below) asynchronously so charges will not be incurred while waiting for all the VMs to spin up.
In the script if Wait-Job
and Receive-Job
include when the script runs all the requested VMs are created, but if Wait-Job
and Receive-Job
are commented out all the VMs are not created. It seems random what VMs are getting created.
Can anyone see what I'm doing wrong with this script?
$VMCount = 5
$Location = 'East US'
$Image = 'MyImage'
$AdminPassword = 'XXXXXXXXXX'
$LinuxUser = 'MyUser'
$InstanceSize = 'ExtraSmall' #extra small only for testing
$CloudServiceName = 'NewAzureMachinePrefix' #this is changed each time to something unique
for ($i = 1; $i -le $VMCount; $i++)
{
$jobId = Start-Job -ArgumentList $CloudServiceName$i, $Location, $Image, $AdminPassword, $LinuxUser, $InstanceSize -ScriptBlock {
param($ServiceName, $Loc, $Img, $Password, $User, $Size)
New-AzureVMConfig -Name $ServiceName -InstanceSize $Size -ImageName $Img |
Add-AzureProvisioningConfig -Linux -LinuxUser $User -Password $Password |
Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel $ServiceName -LUN 0 |
Remove-AzureEndpoint 'SSH' |
Add-AzureEndpoint -Protocol tcp -PublicPort 22 -LocalPort 22 -Name 'SSH' |
Set-AzureSubnet 'MySubnet' |
New-AzureVM -ServiceName $ServiceName -AffinityGroup 'MyGroup' -VNetName 'MyNet'
}
Write-Output $CloudServiceName$i
Wait-Job $jobId
Receive-Job $jobId
}