Azure – Add Multiple Disks to VM

Welcome Forums General PowerShell Q&A Azure – Add Multiple Disks to VM

Viewing 1 reply thread
  • Author
    • #189067
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor


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

      ## connect to Azure
      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’


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




    • #189124
      Topics: 3
      Replies: 223
      Points: 1,221
      Helping Hand
      Rank: Community Hero

      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.

Viewing 1 reply thread
  • The topic ‘Azure – Add Multiple Disks to VM’ is closed to new replies.