将托管数据磁盘附加到虚拟机

Attach managed data disks to a vm

我正在尝试开发一个脚本来将从快照创建的数据磁盘附加到另一个 Azure VM

  1. 从源 VM 创建现有数据磁盘的快照
  2. 从步骤 1 创建的快照创建新的数据磁盘
  3. 将新的 DATADISKS 附加到目标 VM

然而,我在尝试附加磁盘时不断收到错误消息(第 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

代码:


## 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

}

有人可以帮我解决这个错误吗?

如评论所述,错误消息显示您刚刚在此处的代码中输入错误:

$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName disk_name$i

在第二个循环中,您通过在末尾连接循环计数器 $i 来定义变量 $disk_name。那个代码没问题。

但是,在循环内,您试图再次将此循环计数器添加到名称中,但同时使用前面没有 $ 符号的 disk_name。如果没有美元符号,名称将被视为文字字符串。

要使其正常工作,您只需将上面提到的代码行更改为:

$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $disk_name