I am trying to develop a script to attach data diskscreating from snapshots to another Azure 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?
As commented, the error message shows that you have just made a typo in the code here:
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName disk_name$i
Within that second loop, you are defining a variable $disk_name
by concatenating the loopcounter $i
at the end. That code is fine.
However, inside the loop you are trying to add this loopcounter to the name again but at the same time use disk_name
without the $
sign in front. Without the dollar sign, the name is taken as literal string.
All you have to do to make it work is change the above mentiond line of code into:
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $disk_name