Add-AzVMDataDisk 未附加磁盘Add-AzVMDataDisk not attachment disk

Add-AzVMDataDisk 未附加磁盘Add-AzVMDataDisk not attachment disk

我正在尝试将非托管 blob(操作系统磁盘快照的副本)附加到现有 VM 作为数据磁盘。该 VM 目前没有任何数据磁盘。

$vm = Get-AzVM -ResourceGroup "mygroup" -Name "myvm"
$disk = $vm.StorageProfile.OSDisk
$blob = ... bunch of code that creates a snapshot and copies to a blob ...
$diskName = "my-data-disk"
$blobUri = $blob.ICloudBlob.Uri.ToString()

$res = Add-AzVMDataDisk -VM $vm -Name $diskName -CreateOption 'Attach' -VhdUri $destBlobUri -DiskSizeInGB $disk.DiskSizeGB -Lun 0 -Caching 'ReadOnly'
$attached = $res.SotrageProfile.DataDisks

$attached显示为刚刚连接的磁盘,但它没有出现在门户的 VM -> 磁盘 -> 数据磁盘下。

我尝试将操作系统磁盘的快照附加到同一台虚拟机是否存在问题?

我这样做只是为了测试时方便,最终它所连接的虚拟机将成为备份虚拟机。

完整脚本供参考:

Import-Module './lib/RedSky.psm1' -Force

# Load backup config (todo: alow pass config on command line)
# Note, this will make sure we are logged in aswell
$config = Get-RsConfig './backup-config.json'

# Check we can find the VM, bail if not
$vm = Get-RsVM $config.ResourceGroup $config.VirtualMachine
$vm
if (!$vm) {
  write-host "VM not in list of VMs"
  exit 1
}

write-host "Selected VM: $($vm.name) in $($vm.ResourceGroupName), $($vm.Location)"

$destContainer = "vhd-backups"
$destBlobSuffix = "duplicacy-" + (get-date).ToString(‘yyyyMMdd_HHmmss’)

foreach ($backup in $config.Backups) {
  write-host " Source Disk: $($backup.Source.Disk)"
  $disk = Get-RsVMDisk $vm $backup.Source.Disk
  if ($disk -And $disk.Vhd) {
    # Work out storage account, container and blob names from disk URI
    $uri = $disk.Vhd.Uri
    $storageAccount = $uri.split('/')[2].split('.')[0]  # e.g. rg1disks
    $containerName = $uri.split('/')[3]                 # e.g. vhds
    $blobName = $uri.split('/')[-1]                     # e.g. rsa-oz1YYYYMMDDHHMMSS.vhd
    $destBlob = $blobName.split(".vhd")[0] + "-" + $destBlobSuffix + ".vhd"
    $destName = $desbBlobSuffix

    # Log what we are dealing with (change to -verbose at some point)
    write-host "  DiskUri: $uri"
    write-host "  Disk Size: $($disk.DiskSizeGB)"
    write-host "  Resource Group: $($vm.ResourceGroupName)"
    write-host "  Location: $($vm.Location)"
    write-host "  Storage Account: $storageAccount"
    write-host "  Container Name: $containerName"
    write-host "  DiskBlob Name: $blobName"
    write-host "  Dest Container: $descContainer"
    write-host "  Disk Name: $destName"
    write-host "  Dest Blob: $destBlob"

    # Snapshot the disk, copy it, then remove the snapshot
    $storageContext = Get-RsStorageContext $vm.ResourceGroupName $storageAccount
    $snapshot = New-RsSnapshot $storageContext $containerName $blobName
    $blob = Copy-RsSnapshotToBlob $storageContext $snapshot $destContainer $destBlob
    Remove-RsSnapshot $snapshot

    $destBlobUri = $blob.ICloudBlob.Uri.ToString()
    write-host "  Dest Blob URI: $destBlobUri"

    # Attach blob to VM
    $res = Add-AzVMDataDisk -VM $vm -Name $destBlob -CreateOption 'Attach' -VhdUri $destBlobUri -DiskSizeInGB $disk.DiskSizeGB -Lun 1 -Caching 'ReadOnly'
    if ($res -eq $vm) {
      foreach ($attached in $vm.StorageProfile.DataDisks) {
        if ($attached.Name -eq $destBlob) {
          write-host "  Attached Disk: $($attached.Name)"
        }
      }
    }
  }
}

答案1

还需要调用Update-AzVM来提交更改。

相关内容