Add drives

Glenn Maxwell 10,146 Reputation points
2021-06-05T09:28:52.097+00:00

Hi Experts

i have two VMS for which i need to add disks using powershell. The disks should be created and attached to the Server. For Server01 i want to add a new disk with size 100GB. please correct me if the below syntaxes are correct. If Server01 is in Availability Zone1 will the new disk will also be created in Availability Zone1 or do i need to specify it. For Premium Disk is Premium_LRS is right format? for standard SSD and standard HDD what should be the format. will the disk get the tags from the VM?

$vmName = "Server01"
$disk_name = "_data1"
$size = 100
$lun = 0
$vm = Get-AzVM -Nam $vmName
$rgName = $vm.ResourceGroupName
$location = $vm.location 
$storageType = 'Premium_LRS'
$dataDiskName = $vmName + $disk_name
$tags = $vm.Tags
$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $size -tag $Tags
$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun $lun
Update-AzVM -VM $vm -ResourceGroupName $rgName

For Server02 i need to add two disks for disk1 the size is 1TB and for disk2 the size is 2TB. How do i specify the lun size in the below syntax.

$vmName = "Server02"
$disk1_name = "_Data1"
$disk2_name = "_Data2"
$lunl1 = 2
$lunl2 = 3
$lun1size = 1024
$lun2size = 2048


$vm = Get-AzVM -Nam $vmName
$rgName = $vm.ResourceGroupName
$location = $vm.location 
$storageType = 'Premium_LRS'
$dataDisk1Name = $vmName + $disk1_name
$dataDisk2Name = $vmName + $disk2_name
$tags = $vm.Tags
$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $size -tag $Tags 
$dataDisk1 = New-AzDisk -DiskName $dataDisk1Name -Disk $diskConfig -ResourceGroupName $rgName
$dataDisk2 = New-AzDisk -DiskName $dataDisk2Name -Disk $diskConfig -ResourceGroupName $rgName
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk1Name -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun $lunl1
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk2Name -CreateOption Attach -ManagedDiskId $dataDisk2.Id -Lun $lunl2
Update-AzVM -VM $vm -ResourceGroupName $rgName
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,113 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2021-06-06T04:54:34.517+00:00

    Hello @Glenn Maxwell ,
    Below is the complete script for your server02 to add two disks.

    $vmName = "server02"
    $disk1_name = "_Data11"
    $disk2_name = "_Data21"
    $lunl1 = 2
    $lunl2 = 3
    $lun1size = 1024
    $lun2size = 2048
    $vm = Get-AzVM -Nam $vmName
    $rgName = $vm.ResourceGroupName
    $location = $vm.location
    $storageType = 'Premium_LRS'
    $dataDisk1Name = $vmName + $disk1_name
    $dataDisk2Name = $vmName + $disk2_name
    $tags = $vm.Tags
    $diskConfig1 = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $lun1size -tag $Tags -zone 1
    $diskConfig2 = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $lun2size -tag $Tags -zone 1
    $dataDisk1 = New-AzDisk -DiskName $dataDisk1Name -Disk $diskConfig1 -ResourceGroupName $rgName
    $dataDisk2 = New-AzDisk -DiskName $dataDisk2Name -Disk $diskConfig2 -ResourceGroupName $rgName
    $vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk1Name -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun $lunl1
    $vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk2Name -CreateOption Attach -ManagedDiskId $dataDisk2.Id -Lun $lunl2
    Update-AzVM -VM $vm -ResourceGroupName $rgName

    Let me know if you have additional questions !

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,281 Reputation points MVP
    2021-06-06T07:48:06.367+00:00

    Hi @Glenn Maxwell ,

    the host cache parameter is part of the Add-AzVMDataDisk and named -Caching. Possible values are ReadOnly ,ReadWrite ,None.

    https://learn.microsoft.com/en-us/powershell/module/az.compute/add-azvmdatadisk?view=azps-6.0.0#example-1--add-data-disks-to-a-new-virtual-machine

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  2. shiva patpi 13,131 Reputation points Microsoft Employee
    2021-06-05T23:20:15.697+00:00

    Hello @Glenn Maxwell ,
    Thanks for your query and the detailed PowerShell script.
    Regarding your first query "If Server01 is in Availability Zone1 will the new disk will also be created in Availability Zone1 or do i need to specify it"
    -> While creating the data disk you will have to explicitly pass the parameter like -Zone 1 to the command New-AzDiskConfig

    So line11 of your first script will be like below:
    $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $size -tag $Tags -Zone 1
    (or)
    $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $size -tag $Tags -Zone $vm.Zones

    Regarding Second Query "For Premium Disk is Premium_LRS is right format?"
    -> Yes, that's the right format.

    Regarding Third Query "will the disk get the tags from the VM"
    -> Yes , the data disk will inherit whatever tags was mentioned at the VM level based upon your script wrote above

    Regarding Fourth Query "for standard SSD and standard HDD what should be the format"
    -> *Please use Standard_LRS for Standard HDD *
    -> Please use StandardSSD_LRS for Standard SSD

    BTW - Your script syntax is perfectly alright ! I just tested locally - It works like a charm

    Kindly let us know if the above answers all your queries, Make sure to "Upvote and Accept the answer" so that it will help to the community out there who is looking for similar queries

    0 comments No comments

  3. Glenn Maxwell 10,146 Reputation points
    2021-06-06T04:31:47.427+00:00

    For the second script what changes do i need to make
    For Server02 i need to add two disks for disk1 the size is 1TB and for disk2 the size is 2TB. How do i specify the lun size in the below syntax.
    $vmName = "Server02"
    $disk1_name = "_Data1"
    $disk2_name = "_Data2"
    $lunl1 = 2
    $lunl2 = 3
    $lun1size = 1024
    $lun2size = 2048
    $vm = Get-AzVM -Nam $vmName
    $rgName = $vm.ResourceGroupName
    $location = $vm.location
    $storageType = 'Premium_LRS'
    $dataDisk1Name = $vmName + $disk1_name
    $dataDisk2Name = $vmName + $disk2_name
    $tags = $vm.Tags
    $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB $size -tag $Tags
    $dataDisk1 = New-AzDisk -DiskName $dataDisk1Name -Disk $diskConfig -ResourceGroupName $rgName
    $dataDisk2 = New-AzDisk -DiskName $dataDisk2Name -Disk $diskConfig -ResourceGroupName $rgName
    $vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk1Name -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun $lunl1
    $vm = Add-AzVMDataDisk -VM $vm -Name $dataDisk2Name -CreateOption Attach -ManagedDiskId $dataDisk2.Id -Lun $lunl2
    Update-AzVM -VM $vm -ResourceGroupName $rgName

    0 comments No comments

  4. Glenn Maxwell 10,146 Reputation points
    2021-06-06T06:45:49.743+00:00

    Thanks
    One final question what should be the syntax to make host caching Read-only or Read/write.

    0 comments No comments