I am attempting to attach an unmanaged blob, which is a copy of a snapshot of the os disk, to an existing VM as a data disk. The VM doesn't currently have any data disks.

$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 shows as the disk just attached, but it doesn't appear in the portal under VM -> disks -> Data Disks.

Is the fact that I am attempting to attach a snapshot of the OS disk to the same VM an issue?

I am doing that only for convenience while testing at this point, eventually the VM it is attached to will be a backup VM.

The full script for reference:

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
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)"

Need to also call Update-AzVM to commit the changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.