I am trying to develop a script to attach data diskscreating from snapshots to another Azure VM
- Create SNAPSHOTS of existing data disks from source VM
- Create new DATADISKS from the SNAPSHOTS created from step 1
- Attach the new DATADISKS to the destination VM
however I keep getting error when trying to attach the disks(step 3).
Get-AzDisk : The Resource 'Microsoft.Compute/disks/disk_name2' under resource group 'RG-Test' was not found. ErrorCode: ResourceNotFound ErrorMessage: The Resource 'Microsoft.Compute/disks/disk_name2' under resource group 'RG-Test' was not found. ErrorTarget: StatusCode: 404 ReasonPhrase: Not Found OperationID : 67319e0f-3f8f-416a-bb25-9d0547e661a4 Au caractère Ligne:22 : 13 + $disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskNam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError : (:) [Get-AzDisk], ComputeCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureRmDisk Add-AzVMDataDisk : Impossible de valider l'argument sur le paramètre « ManagedDiskId ». L’argument est Null ou vide. Indiquez un argument qui n’est pas Null ou vide et réessayez. Au caractère Ligne:24 : 110 + ... nation_vm_object -CreateOption Attach -ManagedDiskId $disk.Id -Lun $l ... + ~~~~~~~~ + CategoryInfo : InvalidData : (:) [Add-AzVMDataDisk], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.AddAzureVMDataDiskCommand
The code :
## Create Snapshot from a Managed Disk ##
$resourceGroupName = 'RG-Test'
$location = 'east us 2'
$source_vm_name = 'VMS'
$destination_vm_name = 'VMD'
$data_disk_list = Get-azDisk | where {$_.ManagedBy -match $source_vm_name -and $_.OsType -eq $null}
$snapshot_list = New-Object System.Collections.ArrayList($null)
foreach($data_disk_list_iterator in $data_disk_list){
$snapshotName = $destination_vm_name + "_Snapshot_" + $data_disk_list_iterator.Name
$snapshot_config = New-AzSnapshotConfig -SourceUri $data_disk_list_iterator.id -Location $location -CreateOption copy
$snapshot_object = New-AzSnapshot -Snapshot $snapshot_config -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
$snapshot_list.Add($snapshot_object.Id)
}
## Create Managed Data Disk from snapshot created above ##
$i=0
$destination_datadisk_list = New-Object System.Collections.ArrayList($null)
$destination_vm_object = Get-AzVM -Name $destination_vm_name -ResourceGroupName $resourceGroupName
$lun_count = 1
foreach($snapshot_list_iterator in $snapshot_list){
$disk_name = $destination_vm_name + "_datadisk_" + $i
$i += 1
$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot_list_iterator
$datadisk_object = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $disk_name
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName disk_name$i
$destination_vm_object = Add-AzVMDataDisk -VM $destination_vm_object -CreateOption Attach -ManagedDiskId $disk.Id -Lun $lun_count
$lun_count += 1
Update-AzVM -VM $destination_vm_object -ResourceGroupName $resourceGroupName
}
Could someone help me resolve this error?
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName disk_name$i
. Change that to$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $disk_name
– Theo$data_disk_list_iterator
is a variable that contains one item from the collection$data_disk_list
each time the loop is run. (Usually people just call this$i
). Within the loop this variable represents one Managed disk object . – Theo