Azure - Add Multiple Disks to VM

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

  • This topic has 1 reply, 2 voices, and was last updated 3 weeks ago by
    Participant
    .
Viewing 1 reply thread
  • Author
    Posts
    • #189067
      Participant
      Topics: 12
      Replies: 17
      Points: 152
      Rank: Participant

      Hello,

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

      
      ## 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'
      
      

      ``

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

      Thanks,

      Frank

      ``

    • #189124
      Participant
      Topics: 3
      Replies: 136
      Points: 719
      Helping Hand
      Rank: Major Contributor

      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
  • You must be logged in to reply to this topic.