How to get the VHD of an Azure managed disk?

被刻印的时光 ゝ 提交于 2019-12-04 19:40:47

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!