Create Multiple Disks on VM

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

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #168718

    Participant
    Topics: 9
    Replies: 14
    Points: 108
    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 ""
    
    $rgName = ""
    $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: 423
    Points: 676
    Helping Hand
    Rank: Major Contributor

    You repeat lines 9-36 for each disk

  • #168877

    Participant
    Topics: 9
    Replies: 14
    Points: 108
    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: 8
    Replies: 18
    Points: 108
    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: 9
    Replies: 14
    Points: 108
    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: 9
    Replies: 14
    Points: 108
    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: 59
      Points: 328
      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
      }

You must be logged in to reply to this topic.