Search code examples
azurepowershellazure-disk

Copy Azure OSDisks V DataDisks and powershell set Lun at 0


I have a couple of questions

I'm copying some OS disks and some data disks, and I'm using the same process for both, which is create snapshot and then creating disks from the snapshots. I've read a bit online where there's two processes for what appears to be the same task. Is there any difference between the OS Disk and Data Disk when you take a snapshot and create a disk from the snapshot?

Also I'm trying to attach the aforementioned data disks and the LUN number always starts at 1, would anyone know how I can get it to start at 0. The code I have is

$dataDisks = Get-AzDisk | ? {$_.name -like "*$ddisk*"} 
$lun = 0

foreach ($disk in $dataDisks){

$lun += 1

$vm = Add-AzVMDataDisk -CreateOption Attach -VM $vm -Lun $lun -ManagedDiskId $disk.Id
Update-AzVM -VM $vm -ResourceGroupName $VMRG -Verbose

}

Thanks in advance and apologies if this seems like a lot of questions in one post :)


Solution

  • OS disk and data disk snapshots should behave the same as the snapshot is taken of the VM in order to be able to roll-back the changes done on the disks until they're consolidated on the VM.

    Regarding your other issue:

    foreach ($disk in $dataDisks){
    
    $vm = Add-AzVMDataDisk -CreateOption Attach -VM $vm -Lun $lun -ManagedDiskId $disk.Id
    Update-AzVM -VM $vm -ResourceGroupName $VMRG -Verbose
    $i++ # You were modifying the variable before it it had finished. 
    
    }