Forum Discussion
Attach multiple Data disks to Azure VM without removing\overwriting existing Data Disks
Hi Everyone i am trying to achieve "Attach multiple Data disks to Azure VM without removing\overwriting existing Data Disks"
I am able to add multiple data disks to existing vm but i am unable to achieve below condition. Can anyone please help me
IF name=ABC-DataDisk-1
ABC-DataDisk-0 -> Attach
IF name=ABC-DataDisk-2
ABC-DataDisk-0 - Attach
ABC-DataDisk-1 - Attach
IF name=ABC-DataDisk-3
ABC-DataDisk-0 - Attach
ABC-DataDisk-1 - Attach
ABC-DataDisk-2 - Attach
IF name=ABC-DataDisk-4
ABC-DataDisk-0 - Attach
ABC-DataDisk-1 - Attach
ABC-DataDisk-2 - Attach
ABC-DataDisk-3 - Attach
# STEP 1: Sign-in to Azure via Azure Resource Manager
Connect-AzAccount
# STEP 2: Select Azure Subscription
$subscriptionId =
( Get-AzSubscription |
Out-GridView `
-Title "Select an Azure Subscription …" `
-PassThru
).SubscriptionId
Get-AzSubscription -SubscriptionId $subscriptionId | Select-AzSubscription
$rgName = 'DEVNETWORK-RG'
$vmName = 'test-vm'
$location = 'West Europe'
$storageType = 'Standard_LRS'
$zone = '1'
$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName
for ($i = 1; $i -lt 3; $i++) {
$dataDiskName = $vmName + "_datadisk$i"
$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Empty -DiskSizeGB "1024" -Zone $zone
$dataDisk = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk.Id -Lun $i -Caching None
}
Update-AzVM -VM $vm -ResourceGroupName $rgName