Asked by:
Best way to create Azure VMs simultaneously? Start-Job? ForEach Parallel?

General discussion
-
I'm trying to create Azure VMs simultaneously from a list. Maybe there is a better way to do this, but I'm trying to do this using a foreach loop and PowerShell jobs. I keep running into issues each way I try. If I run the script without jobs, it works but I have to wait for each VM to be created before the next one. I'd like to have all VMs created at the same time if possible.
Here's what I have so far:
$func = { $VMLocalAdminUser = "User" $VMLocalAdminSecurePassword = ConvertTo-SecureString "PasswordWhatever" -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword) #using this below function to generate a unique name for each VM. I'm sure there's a better way for this. function get-randomstring { [CmdletBinding()] param ( [int]$length) $rca = 1..$length | foreach { $ran = Get-Random -Minimum 97 -Maximum 123 [char][byte]$ran } $rca -join '' } } foreach ($image in $imagelist) { $scriptBlock = { New-AzureRmVm -Credential $Credential -ResourceGroupName "RG1445" -Name (get-randomstring -length 10).ToUpper -ImageName $image -Location "West US 2" -Size "Standard_DS2_v2" -VirtualNetworkName "ImageMaker" -SubnetName "ImageMakerSubnet" -OpenPorts 3389 } } Start-Job -InitializationScript $func -ScriptBlock $scriptBlock
What I get is:
PS C:\ Get-Job | Receive-Job
WARNING: New-AzureRmVM: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return Standard_LRS and Premium_LRS
Object reference not set to an instance of an object.
+ CategoryInfo : CloseError: (:) [New-AzureRmVM], NullReferenceException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
+ PSComputerName : localhostI also tried putting -AsJob on the end of the New-AzureRmVm, but end up with the same error. I also tried adding Start-Job to the front of the command within the foreach loop and that didn't work either.
Maybe there's a better way to go about all of this. Thanks so much for your help!
- Edited by BusyLifeSoftwareMLW Sunday, June 16, 2019 11:25 AM Didn't use code tool.
Sunday, June 16, 2019 3:20 AM
All replies
-
Please post your script correctly using the code posting tool provided. What you have posted is unreadable.
\_(ツ)_/
Sunday, June 16, 2019 4:51 AM -
Sorry I didn't know about that feature. I just fixed it and edited my post. Thanks for letting me know. :)Sunday, June 16, 2019 11:26 AM
-
Scripts need to be correctly formatted and indented. If you do this you will see the issues that your script has. Formatting and indenting will make the error obvious.
\_(ツ)_/
Sunday, June 16, 2019 12:47 PM -
I don't know much about Azure... One thing though for starters:
foreach ($image in $imagelist)
What is $imagelist? It's not defined in your code...
I guess also... Your Start-Job is outside of your foreach loop. I doubt that's going to work, unless you only have one single $image variable, otherwise it's only the last in $imagelist that will actually be run/used.
Sunday, June 16, 2019 4:56 PM