Azure - Add Multiple Disks to VM

Hello,

I have the following script which allows me to only add 1 disk, even though I have a FOR Loop.

[pre]

connect to Azure

Disconnect-AzAccount
Connect-AzAccount

Set-AzContext -Subscription “azSubName”

$numDisks = Read-Host “Enter the number of disks to add”

$rgName = “rgname”
$vmName = “server”
$diskName = $vmName.Substring(0,12) + ‘_’ + $diskNum

for ($i=1; $i -le $numDisks; $i++) {
$diskNum = Read-Host “Enter the disk number”
$diskSize = Read-Host “Enter size of disk”
$lunNum = Read-Host “Enter the LUN number”

create the initial configuration

$diskConfig = New-AzDiskConfig `
-Location “location” `
-CreateOption Empty `
-DiskSizeGB $diskSize

create the data disk

$dataDisk = New-AzDisk `
-ResourceGroupName $rgName `
-DiskName $diskName `
-Disk $diskConfig

get the VM where you want to add the data disk

$vm = Get-AzVM -ResourceGroupName $rgName `
-Name $vmName

add the data disk to the VM configuration

$vm = Add-AzVMDataDisk `
-VM $vm `
-Name $diskName `
-CreateOption Attach `
-ManagedDiskId $dataDisk.Id `
-Caching “ReadWrite” `
-Lun $lunNum
}

update the VM

Update-AzVM -ResourceGroupName $rgName -VM $vm

prepare the data disks

#Get-AzDisk -DiskName $diskName | Where partitionstyle -EQ ‘raw’

[/pre]``

Any idea on how this loop can be changed to allow the addition of multiple disks to a VM?

Thanks,

Frank

``

You need to run the Update-AzVM inside the for loop.

I am not sure we can add disk array to a VM, but one at a time we can add.

Thank you.