Welcome Forums General PowerShell Q&A Create Multiple Disks on VM

Viewing 5 reply threads
  • Author
    Posts
    • #168718
      Participant
      Topics: 16
      Replies: 29
      Points: 226
      Rank: Participant

      Hello,

      I have a basic script where I can create one disk on a VM.  I’d like to be able to create multiple disk with different sizes and names.  I’m unsure how to get started on this.

      This is what I have so far:

      
      ## connect to Azure
      Connect-AzAccount
      
      Set-AzContext -Subscription “<sub_name>”
      
      $rgName = “<rg_name>”
      $vmName = “azr-tst-01”
      $diskName = $vmName.Substring(0,12) + “_Data”
      
      # create the initial configuration
      $diskConfig = New-AzDiskConfig `
      -Location “EastUS2” `
      -CreateOption Empty `
      -DiskSizeGB 100
      
      # 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 `
      -Lun 1
      
      # update the VM
      Update-AzVM -ResourceGroupName $rgName -VM $vm
      
      # prepare the data disks
      #Get-AzDisk -DiskName $diskName | Where partitionstyle -EQ ‘raw’
      
      

       

      Thanks,

      Frank

    • #168727
      Participant
      Topics: 9
      Replies: 431
      Points: 738
      Helping Hand
      Rank: Major Contributor

      You repeat lines 9-36 for each disk

    • #168877
      Participant
      Topics: 16
      Replies: 29
      Points: 226
      Rank: Participant

      A for loop as such (just an example) before 9 and after 36:

      $disks = 4
      $vmName = “azr-tst-01”
      $diskName = $vmName.Substring(0,12) + “_Data”

      for ($i=1; $i -le $disks; $i++) {
      Write-Host $diskName $i
      }

      But how do I have a unique name for each disk?  I’d like to add a special name like data, log, etc for each disk, not just a number.

       

    • #168976
      Participant
      Topics: 9
      Replies: 26
      Points: 160
      Helping Hand
      Rank: Participant

      Then you could create a loop for each of those names, or in the loop, have disk 1 be Data, 2 be logs and so on.
      Example: IF ($I -eq 1) {$Diskname = Data}

    • #169033
      Participant
      Topics: 16
      Replies: 29
      Points: 226
      Rank: Participant

      Thanks for that!

      i.e.)

      
      $disks = 4
      $vmName = “azr-tst-01”
      $diskName = $vmName.Substring(0,12) + ‘_00’
      
      for ($i=1; $i -le $disks; $i++) {
      
      if ($i -eq 1) {
      Write-Host ($diskName + $i)
      }
      elseif ($i -eq 2) {
      Write-Host ($diskname + $i)
      }
      else {
      Write-Host “No more disks left”
      }
      }
      
      

      How would you incorporate varying disk sizes?

    • #169045
      Participant
      Topics: 16
      Replies: 29
      Points: 226
      Rank: Participant

      This would be the loop, but I’m unsure how I incorporate the if statement:?

      
      for ($i=1; $i -le $numDisks; $i++) {
      # create the initial configuration
      $diskConfig = New-AzDiskConfig `
      -Location “EastUS2” `
      -CreateOption Empty `
      -DiskSizeGB 100
      
      # 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 `
      -Lun 1
      }
      
      
      • #169171
        Participant
        Topics: 1
        Replies: 63
        Points: 356
        Helping Hand
        Rank: Contributor

        It would probably be easier to create a csv file, and pull that as variables into your script.

        $disks=(import-csv Disks.csv)
        foreach ($disk in $disks){
        your script logic
        }
Viewing 5 reply threads
  • The topic ‘Create Multiple Disks on VM’ is closed to new replies.