question

GlennMaxwell-2309 avatar image
0 Votes"
GlennMaxwell-2309 asked AndreasBaumgarten edited

Add drives

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-machinesazure-virtual-machines-images
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered

Hello @GlennMaxwell-2309 ,
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 !

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered

Hello @GlennMaxwell-2309 ,
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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GlennMaxwell-2309 avatar image
0 Votes"
GlennMaxwell-2309 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GlennMaxwell-2309 avatar image
0 Votes"
GlennMaxwell-2309 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @GlennMaxwell-2309 ,

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

https://docs.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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GlennMaxwell-2309 avatar image
0 Votes"
GlennMaxwell-2309 answered AndreasBaumgarten commented

in the below syntax if i dont specify host cache by default will it take none? . if i need to specify the host cache readonly on disk2 is the below syntax correct


 $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
 $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 -Caching 'ReadOnly'
 Update-AzVM -VM $vm -ResourceGroupName $rgName
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Based on the linked documentation the default is ReadWrite (if you don't specify the parameter -Caching)

https://docs.microsoft.com/en-us/powershell/module/az.compute/add-azvmdatadisk?view=azps-6.0.0#parameters

The syntax looks. good for me, but not tested by myself.


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

Regards
Andreas Baumgarten

0 Votes 0 ·