SaphuA
SaphuA

Reputation: 3140

How to get the VHD of an Azure managed disk?

I have created a VM with a managed disk. Managed disks are no longer stored into the blob storage by default. Problem is that I now need the vhd file of the osdisk, but I am unable to find a proper way to retrieve it.

The only method I found is to open the disk in the azure portal and press Export to create a download link to the vhd file. This method is undesired.

Upvotes: 3

Views: 4937

Answers (3)

DevUser
DevUser

Reputation: 751

You can copy/export the VHD of a managed disk to a storage account with PowerShell

#Connect to Azure and set your azure subscription

#Declare Variables
$resourceGroupName = 'xxxxx-rg'
$snapshotName = 'xxxxxx.md'
$resourceGroupNameStorageAccount = 'xxxx-rg'
$storageAccountName = 'xxxx-storage'
$storageContainerName = 'xxxxx'
$destinationVHDFileName = 'xxxxxx.vhd'

#Get the Storage Account Key of the Destination Storage Account
$storageAccountKey = Get-AzStorageAccountKey -resourceGroupName $resourceGroupNameStorageAccount -AccountName $storageAccountName

#Generate the SAS for the snapshot
$sas = Grant-AzSnapshotAccess -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -DurationInSecond 3600 -Access Read

#Create the context of the destination storage account for the snapshot
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey ($storageAccountKey).Value[0]

#Copy the snapshot to the destination Storage Account
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName

Upvotes: 3

Jon
Jon

Reputation: 9

Make sure your AzureRM Powershell Module is up to date:

Install-Module AzureRM -allowclobber -force

Your Set-AzureRMVMOSDisk command should now have -ManagedDiskID. Just enter the resource ID for the managed disk and it should work.

Example

$NewVM = New-AzureRMVMConfig -VMName VMName - VMSize "Standard_A1_V2"
Set-AzureRMVMOSDisk -VM $NewVM -Name "DiskName" -CreateOption Attach -Caching ReadWrite -Windows -ManagedDiskID "ManagedDiskResourceID"
New-AzureRmVM -ResourceGroupName "ResourceGroupName" -VM $NewVM -Location CanadaEast

Upvotes: 0

Michael C
Michael C

Reputation: 99

For managed disks you do not use a vhd. Rather, for the disk section, you use a template like this

"osDisk": {
    "osType": "Linux",
    "name": "[parameters('VMName')]",
    "createOption": "FromImage",
    "caching": "ReadWrite",
    "managedDisk": {
        "id": "ManagedDiskID"
    }
}

You reference the disk by managed disk ID rather than uri

Upvotes: -2

Related Questions