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

问题描述 投票:0回答:1

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

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

但是尝试连接磁盘时,我总是收到错误消息(步骤3)。

Get-AzDisk:找不到资源组“ RG-Test”下的资源“ Microsoft.Compute / disks / disk_name2”。错误代码:ResourceNotFound错误消息:找不到资源组“ RG-Test”下的资源“ Microsoft.Compute / disks / disk_name2”。ErrorTarget:状态码:404ReasonPhrase:未找到操作ID:67319e0f-3f8f-416a-bb25-9d0547e661a4利加(Ligne)22:13+ $ disk = Get-AzDisk -ResourceGroupName $ resourceGroupName -DiskNam ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~+ CategoryInfo:CloseError:(:) [Get-AzDisk],ComputeCloudException+ FullyQualifiedErrorId:Microsoft.Azure.Commands.Compute.Automation.GetAzureRmDiskAdd-AzVMDataDisk:不可能通过参数“ ManagedDiskId”验证参数。 L'argument est Null ou vide。毫无争议地争论不休。里加(Ligne)24:110+ ... national_vm_object -CreateOption附加-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

}

有人可以帮助我解决此错误吗?

azure powershell azure-virtual-machine azure-powershell azure-managed-disk
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.