Azure PowerShell Runbook Get Set Az-Disk dynamically - azure

The team is trying to automate a snapshot restore, which was achieved successfully. However I am not able to figure out how to dynamically get the previous disk within the resource group; As well as set the next disk with a new name.
In the code below "AZR-001_OsDisk_7" has to set dynamically to "AZR-001_OsDisk_8" the next time it runs:
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName "AZR-001_OsDisk_7"
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name "AZR-001_OsDisk_7"

Not a final solution, but I have a quick idea. You may use Get-AzDisk -ResourceGroupName 'ResourceGroupName ' to get all the disks. And then you can get the disk name.
As you named the disk with appropriate rule, you may split the name string by _, then you will get a string array which contains all the parts. In this way, you will be able to get the version.
A sample:
$disks = Get-AzDisk -ResourceGroupName JackVS2019
foreach($disk in $disks){
$arr = $disk.Name.Split('_')
Write-Host $arr[2]
}
The output:
1
Then you can generate the new disk name.

$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$account = Connect-AzAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Select-AzSubscription -SubscriptionName 101-Prototyping
$vm = Get-AzVM -ResourceGroupName "ETD-RFT" -Name "AZR-101-007-001"
$snapshot = Get-AzSnapshot -ResourceGroupName "ETD-RFT" -SnapshotName "SNAPSHOT-DEC-2019"
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disks = Get-AzDisk -ResourceGroupName "ETD-RFT"
$attached = $disks | Where-Object ManagedBy -ne $null
foreach($disk in $attached)
{
$arr = $disk.Name.Split('_')
$arr[2]
}
$a = $arr[2] -as [int]
$a=$a+1
Write-Host $a
$newname = $arr[0] + "_" + $arr[1] + "_" + $a
$disknew = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName $newname
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name $newname
Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk1.Id -Name $disk1.Name
Update-AzVM -ResourceGroupName "ETD-RFT" -VM $vm

Related

Azure VMs fails as public ip is allocated to other resource

I am using a powershell script to create multiple Vms based on an image. The first Vm is ok but when attempting the second Vm I get an error saying that :
| Resource /subscriptions/....../networkInterfaces/xxxxx/ipConfigurations/xxxxx is referencing public IP address
| /subscriptions/xxxxxxxxx/providers/Microsoft.Network/publicIPAddresses/Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress that is already allocated to
| resource /subscriptions/......./networkInterfaces/xxxxx/ipConfigurations/xxxxx.
Here is the script I am using:
param(
[string] $WeekNo="NoWeek",
[int] $VmCount=0
)
#$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
## VM Account
# Credentials for Local Admin account you created in the sysprepped (generalized) vhd image
$VMLocalAdminUser = "xxxxx"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "xxxxxxx" -AsPlainText -Force
$image = "/subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Compute/images/xxxxxxxxx"
## Azure Account
$LocationName = "SwedenCentral"
$ResourceGroupName = "xxxx_" + $WeekNo
if( -Not( Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorAction Ignore)) {
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName
Write-Host "ResourceGroup" $ResourceGroupName "created"
$VMSize = "Standard_B2ms"
## Networking
$NetworkName = "xxxxxx_" + $WeekNo + "_net" # "MyNet"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"
$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
}
$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);
$VMName = "xxxx" + $WeekNo
##New-AzVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose -Image $image
for($i=1; $i -le $VmCount; $i++){
$VMBaseName = "iCPSEDU" + $WeekNo + $i
$StorageAccount = "xxxxx" + $WeekNo + $i
$PublicIPAddressName = $VMBaseName + "PIP$(Get-Random)"
$NICName = $VMBaseName + "NIC"
$DNSNameLabel = "xxxx" + $WeekNo + $i + "dns" # mydnsname.westus.cloudapp.azure.com
$PIP = New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod Dynamic
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $PIP.Id
Write-Host "Creating VM " $VMBaseName
New-AzVm `
-ResourceGroupName $ResourceGroupName `
-Name $VMBaseName `
-ImageName $image `
-Location $LocationName `
-VirtualNetworkName $Vnet `
-SubnetName $SubnetName `
-SecurityGroupName "myImageNSG" `
-PublicIpAddressName $PIP -Credential $Credential -Size $VMSize -PublicIpSku Standard
Write-Host "VM " $VMBaseName " Created"
Stop-AzVM -ResourceGroupName $ResourceGroupName $VMBaseName -Force -NoWait
Write-Host "VM " $VMBaseName " Stopped"
}
Write-Host "Done."`
To me it seems that the variable used for the PIP is not "flushed" properly between the executions but I have no idea on how to do this?
Or is there something else causing the error?
I have tried adding some delays but without effect.
Create a public IP address and specify a DNS name
Create a NSG
Create a NIC and associate with created pub IP address and NSG
Create a virtual machine configuration and assign the NIC
Create the VM with the config
https://github.com/Azure/azure-docs-powershell-samples/blob/master/virtual-machine/create-vm-detailed/create-windows-vm-detailed.ps1
rough summary of important steps:
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
-Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_D1 | `
Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzVMNetworkInterface -Id $nic.Id
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
MS is providing well tested powershell code for various tasks:
I prefer the github samples https://github.com/Azure/azure-docs-powershell-samples over the steps in learn and doc.microsoft.com
also have a deeper look at the Azure CLI examples and template based deployments. It seems to me that MS is abandoning PS a bit.

Azure-DevOps Run powershell Getting Error while running

I have below command which is used for change vm size on azure:
Install-PackageProvider -name nuget -MinimumVersion 2.8.5.201 -Force
IF(!(Get-InstalledModule PSExcel -ErrorAction SilentlyContinue)){
Install-Module -Name PSExcel -ErrorAction SilentlyContinue -Force
Import-Module PSExcel -Force
}
IF(!(Get-InstalledModule ImportExcel -ErrorAction SilentlyContinue)){
Install-Module -Name ImportExcel -ErrorAction SilentlyContinue -Force
Import-Module ImportExcel -Force
}
IF(!(Get-InstalledModule az -ErrorAction SilentlyContinue)){
Install-Module -Name az -ErrorAction SilentlyContinue -Force
Import-Module az -Force
}
Import-Module PSExcel -Force
Import-Module ImportExcel -Force
$ExcelFile = "D:\test-data (3).xlsx"
$objExcel = New-Excel -Path $ExcelFile
$WorkBook = $objExcel | Get-Workbook
$NewExcelFile ="D:\test-data1.xlsx"
copy-item $ExcelFile -Destination $NewExcelFile
$sheetName = "Sheet2"
$newExcel = New-Object -ComObject Excel.Application
$newwb = $newExcel.Workbooks.Open($NewExcelFile)
$WorkSheet = $newwb.sheets.item($sheetName)
$WorksheetRange = $workSheet.UsedRange
$RowCount = $WorksheetRange.Rows.Count
$sheet=$newwb.Worksheets.Item(1)
$q=$RowCount
write-host 260
$q1="R"+$q
$r1 = $sheet.Range("R2:$q1")
$r1.cut()
$i1="I"+$q
$r2 = $sheet.Range("I2:$i1")
$sheet.Paste($r2)
$newwb.Close($true)
$newExcel.Quit()
#Import-Module az -Force
#Connect-AzAccount
Write-Host 273
#Loop through all items in the excel
ForEach($Worksheet in #($Workbook.Worksheets))
{
$totalNoOfRecords = $Worksheet.Dimension.Rows
$totalNoOfItems = $totalNoOfRecords - 1
# Declaring starting positions first row and column names
$rowNo, $Instance_Type_Current = 1, 9
$rowNo, $Instance_Type_Recommended = 1, 18
$rowNo, $Resource_Group= 1,42
$rowNo, $Resource_Name = 1,2
$rowNo, $osname = 1,8
if ($totalNoOfRecords -gt 1)
{
#Loop to get values from excel file
for ($i = 1; $i -le $totalNoOfRecords - 1; $i++)
{
$newvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Recommended).text
$vmName = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Name).text
$resourceGroup = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Group).text
$currentvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Current).text
$Checkwindowsos = $WorkSheet.Cells.Item($rowNo + $i, $osname).text
$currentsizefromazure = (Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue).HardwareProfile.VmSize
#If only windows OS then VM size will update on azure
IF($Checkwindowsos -match 'windows'){
IF($currentvmsize -eq $currentsizefromazure) {
Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue
$vm.HardwareProfile.VmSize = $newvmsize
Write-Host 304
$vmstatus = $null
while($vmstatus -notmatch 'deallocated'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
#Start VM Snapshot(Backup)
$vm1 = get-azvm -Name $VmName -ResourceGroupName $resourceGroup
write-host $VmName
$snapshotdisk = $vm1.StorageProfile
$vmlocation =$vm1.location
#backup OS DISK
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location $vmlocation -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig
Write-Output "VM-Name $($vm1.name) OS Disk Snapshot End & Snapshot name is $snapshotNameOS"
Write-Host 319
#backup Data DISK
$dataDisks = ($snapshotdisk.DataDisks).name
IF($dataDisks -ne $null){
foreach ($datadisk in $datadisks) {
$dataDisk = Get-AzDisk -ResourceGroupName $vm1.ResourceGroupName -DiskName $datadisk
Write-Output "VM $($vm1.name) data Disk Name $($datadisk.Name) Snapshot Begin"
$DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location $vmlocation
$snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig
Write-Output "VM $($vm1.name) data Disk $($datadisk.Name) Snapshot End"
}}
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup -ErrorAction SilentlyContinue
Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName -ErrorAction SilentlyContinue
$vmstatus = $null
while($vmstatus -notmatch 'running'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
Write-Host Your VM name $vmName is started and changed size from $currentvmsize changed to $newvmsize
}
elseif($currentsizefromazure -eq $newvmsize) { Write-host Your VM $vmName is already Upgraded}
else{Write-host VM current size is mismatched from azure size}
}
}
}
}
Giving below error while running this script on azure devops:
Please help to resolve error.
Unable to get the Open property of the Workbooks class
At C:\Users\azureadmin\Downloads\vsts-agent-win-x64-2.195.2\_work\1\s\Automatic VM backup.ps1:28 char:1
+ $newwb = $newExcel.Workbooks.Open($NewExcelFile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Note: Script is running on self hosted agent and this script is running when normal ISE also azconnection through devops azure
p.s. This script is change vm size and get backup

Install ssl certificate from azure key vault with Automation Accounts

i have some issues.
I have in key vault some SSL certs from LetsEncrypt,and i want to automate installing certificates to VM in azure. I create runbook with some code:
$connectionName = "AzureRunAsConnection"
try
{# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}$certUrl = (Get-AzureKeyVaultSecret -VaultName "MyVault" -Name "letsecrypt-my-cert").Id;
$vm=Get-AzureRmVM -ResourceGroupName "MyRS" -Name "MyVm"
$vaultId=(Get-AzureRmKeyVault -VaultName "MyVault").ResourceId
$certStore = "MySSL"
$vm = Add-AzureRmVMSecret -VM $vm -SourceVaultId $vaultId -CertificateStore $certStore -CertificateUrl $certURL
Update-AzureRmVM -ResourceGroupName "MyRS" -VM $vm
It install ssl to my VM, but i want to check some parameters of certificate and run this script, for example: if certificate updated run install to vm and if not do nothing. What parameters i must use and how check them. Maybe someone do similar task?
I solve this issues with these code modification:
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$certUrl = (Get-AzureKeyVaultSecret -VaultName "MyVault" -Name "letsecrypt-MyCert").Id
$vm=Get-AzureRmVM -ResourceGroupName "MyRG" -Name "MyVm"
$certUrlOnVM=$vm.osProfile.secrets.vaultCertificates.certificateUrl
$vaultId=(Get-AzureRmKeyVault -VaultName "MyVault").ResourceId
$curentDate = Get-Date
$d = $curentDate.day
$m = $curentDate.month
$y = $curentDate.year
$curentDateStr = "$d/$m/$y"
$certStore = "MySSL_"+$curentDateStr
if ($certUrl.equals($certUrlOnVM)){
"Certificate already installed on VM"
} else {
Get-AzureRmVM -ResourceGroupName "MyRG" -Name "MyVm" | Remove-AzureRmVMSecret | Update-AzureRmVM
$vm = Add-AzureRmVMSecret -VM $vm -SourceVaultId $vaultId -CertificateStore $certStore -CertificateUrl $certURL
Update-AzureRmVM -ResourceGroupName "MyRG" -VM $vm
"Certificate installed on VM successfully"
}
I add some variables for get certificate url in key vault and section certificateUrl from OSProfile on vm. I check if cert in keyvault and vm are identical, i do nothing, if not i do remove old section on vm and install new cert.

Azure Start/Stop VM via webhook

We have multiple VM's in our azure environment with multiple resourcegroups. Some of the resourcegroups have multiple VM's. We are now using an URL triggers webhook that will start or stop VM's. This is working, but when a resourcegroup contains multiple VM's all the VM's will start or all the VM's will stop instead of the one you want to start/stop.
Tried multiple scripts but it's isn't working or give me errors.
param(
[Parameter(Mandatory=$false)]
[object]
$WebHookData
)
write output "Data WebHook $WebHookData"
#retrieve ResourceGroup
$ResourceGroupName = $WebHookData.RequestBody
write output "Data ResourceGroup $ResourceGroupName"
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
$VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName
if(!$VMs)
{
Write-Output -InputObject 'No VMs were found in the specified Resource Group.'
}
else
{
ForEach ($VM in $VMs)
{
$StartVM = Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -Force #-ErrorAction SilentlyContinue
}
}
$message = ConvertTo-Json -Compress -InputObject ([ordered]#{
headers = #{'content-type' = 'text/plain'}
body = ''
statusCode = 200
})
You could try below script for Start/Stop Virtual machine.
Start VM
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$null = Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$VMs = Get-AzureRmResource|Where-Object {$_.Tags.Keys -eq "owner" -and $_.Tags.Values -eq "daneum"}
foreach ($VM in $VMs) {
if ($VM.ResourceType -eq "Microsoft.Compute/virtualMachines") {
Start-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Verbose
}
}
Stop VM
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$null = Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$VMs = Get-AzureRmResource|Where-Object {$_.Tags.Keys -eq "owner" -and $_.Tags.Values -eq "daneum"}
foreach ($VM in $VMs) {
if ($VM.ResourceType -eq "Microsoft.Compute/virtualMachines") {
Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force -Verbose
}
}
For webhook integration procedure you could take a look here

Multiple VM Creation by ARM Powershell approach

I have a ps workflow(.psm file) where I am trying to create 5 vms in parallel. I am using ARM cmdlets.I am getting an error-
Error- Cannot validate argument on parameter 'SubnetId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command
again.
Here is my challange-
Even if I remove -parallel parameter from foreach even then its not making any difference.
If I run the same code NOT inside a workflow(ps1 file) removing -parralel parameter I am able to
create 5 vms
Code-
workflow Create-VMs
{
$UserName = "abc#cde.onmicrosoft.com"
$pwd = ConvertTo-SecureString "xxxxxxxx" -AsPlainText -Force
$AzureCredential = New-Object System.Management.Automation.PSCredential($UserName, $pwd)
login-azurermaccount -credential $AzureCredential
Add-AzureRmAccount -Credential $AzureCredential
Select-AzureRmSubscription -SubscriptionName "xxxxx"
$virtualNetworkName = "myvpn"
$locationName = "East US"
$ResourceGroupName = "myrg"
$user = "adminuser"
$password = "AdminPass123"
$VMSize = "Standard_D2"
$sourcevhd = "https://abc.blob.core.windows.net/vhds/windowsserver2008.vhd"
$virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $virtualNetworkName
foreach -parallel($i in 1..5)
{
$VMName = "myname" + $i
$destinationVhd = "https://abc.blob.core.windows.net/vhds/windowsserver2008" + $i + ".vhd"
$staticip = "dynamicip" + $i
$virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $virtualNetworkName
$publicIp = New-AzureRmPublicIpAddress -Name $staticip -ResourceGroupName $ResourceGroupName -Location $locationName -AllocationMethod Dynamic
$networkInterface = New-AzureRmNetworkInterface -ResourceGroupName $ResourceGroupName -Name $VMName -Location $locationName -SubnetId $virtualNetwork.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$vmConfig = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $VMName -VhdUri $destinationVhd -CreateOption FromImage -Windows -SourceImageUri $sourcevhd
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterface.Id
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)
Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -Credential $cred -ProvisionVMAgent -ComputerName $VMName
New-AzureRmVM -VM $vmConfig -Location $locationName -ResourceGroupName $ResourceGroupName
}
}
Not able to find out what is the actual problem. Any other approach for creating multiple vms in parallel using ARM ?
I've not had much luck with -parallel and the Azure cmdlets, but I can think of two options for you:
1) use PowerShell jobs - this would require you to login for each job, here's a snippet of how I do it - the "job" in this case is removing a resource group.
foreach ($AzureResourceGroup in $AzureResourceGroups) {
Start-Job -Name $AzureResourceGroup {
Param($AzureResourceGroup, $creds, $tenantID, $subscriptionName)
Login-AzureRmAccount -ServicePrincipal -Credential $creds -TenantId $tenantId
Select-AzureRmSubscription -SubscriptionName $subscriptionName
Remove-AzureRMResourceGroup -Force -Verbose -Name $AzureResourceGroup
} -ArgumentList $AzureResourceGroup, $creds, $tenantId, $subscriptionName
}
Get-Job | Wait-Job | Receive-Job
2) I think better way would be to use a JSON template, and have Azure orchestrate the deployment. There's a sample of how of the template and how to do it here: https://github.com/bmoore-msft/AzureRM-Samples/tree/master/VMFromImageMulti - there's a readme in the root to show how to deploy it.

Resources