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 :)

Norrin Rad
  • 881
  • 2
  • 18
  • 42

2 Answers2

1

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. 

}
alexzelaya
  • 255
  • 1
  • 7
0

I have been doing a lot of subscription migrations and need to be able to move and attach data disks, pretty similar.

I always just start mine with this:

$lun = -1

## If multiple Data Disks it will prompt for the LUN, start at 0
foreach ($ddisk in $datadisks) {
    $luns = ++$lun
$getddisk = Get-AzDisk -ResourceGroupName $destinationrg -DiskName $ddisk
Write-output $ddisk.id
$newvm = Add-AzVMDataDisk -vm $newvm -Name $ddisk -CreateOption Attach - 
ManagedDiskId $getddisk.id -Caching Readonly -storageaccounttype 
Premium_LRS -Lun $luns
     }