Attach multiple Data disks to Azure VM without removing\overwriting existing Data Disks

%3CLINGO-SUB%20id%3D%22lingo-sub-2465388%22%20slang%3D%22en-US%22%3EAttach%20multiple%20Data%20disks%20to%20Azure%20VM%20without%20removing%5Coverwriting%20existing%20Data%20Disks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2465388%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Everyone%20i%20am%20trying%20to%20achieve%20%22Attach%20multiple%20Data%20disks%20to%20Azure%20VM%20without%20removing%5Coverwriting%20existing%20Data%20Disks%22%3C%2FP%3E%0A%3CP%3EI%20am%20able%20to%20add%20multiple%20data%20disks%20to%20existing%20vm%20but%20i%20am%20unable%20to%20achieve%20below%20condition.%20Can%20anyone%20please%20help%20me%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EIF%20name%3DABC-DataDisk-1%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-0%20-%26gt%3B%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EIF%20name%3DABC-DataDisk-2%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-0%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-1%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EIF%20name%3DABC-DataDisk-3%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-0%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-1%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-2%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EIF%20name%3DABC-DataDisk-4%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-0%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-1%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-2%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CEM%3EABC-DataDisk-3%20-%20Attach%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20STEP%201%3A%20Sign-in%20to%20Azure%20via%20Azure%20Resource%20Manager%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20STEP%202%3A%20Select%20Azure%20Subscription%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24subscriptionId%20%3D%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E(%20Get-AzSubscription%20%7C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EOut-GridView%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Title%20%22Select%20an%20Azure%20Subscription%20%E2%80%A6%22%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-PassThru%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E).SubscriptionId%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGet-AzSubscription%20-SubscriptionId%20%24subscriptionId%20%7C%20Select-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24rgName%20%3D%20'DEVNETWORK-RG'%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24vmName%20%3D%20'test-vm'%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24location%20%3D%20'West%20Europe'%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24storageType%20%3D%20'Standard_LRS'%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24zone%20%3D%20'1'%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24vm%20%3D%20Get-AzVM%20-Name%20%24vmName%20-ResourceGroupName%20%24rgName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSTRONG%3Efor%20(%24i%20%3D%201%3B%20%24i%20-lt%203%3B%20%24i%2B%2B)%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24dataDiskName%20%3D%20%24vmName%20%2B%20%22_datadisk%24i%22%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24diskConfig%20%3D%20New-AzDiskConfig%20-AccountType%20%24storageType%20-Location%20%24location%20-CreateOption%20Empty%20-DiskSizeGB%20%221024%22%20-Zone%20%24zone%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24dataDisk%20%3D%20New-AzDisk%20-DiskName%20%24dataDiskName%20-Disk%20%24diskConfig%20-ResourceGroupName%20%24rgName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24vm%20%3D%20Add-AzVMDataDisk%20-VM%20%24vm%20-Name%20%24dataDiskName%20-CreateOption%20Attach%20-ManagedDiskId%20%24dataDisk.Id%20-Lun%20%24i%20-Caching%20None%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EUpdate-AzVM%20-VM%20%24vm%20-ResourceGroupName%20%24rgName%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

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

0 Replies