Create a managed disk from a VHD file in a storage account in same or different subscription with PowerShell
This script creates a managed disk from a VHD file in a storage account in same or different subscription. Use this script to import a specialized (not generalized/sysprepped) VHD to managed OS disk to create a virtual machine. Also, use it to import a data VHD to managed data disk.
Don't create multiple identical managed disks from a VHD file in small amount of time. To create managed disks from a vhd file, blob snapshot of the vhd file is created and then it is used to create managed disks. Only one blob snapshot can be created in a minute that causes disk creation failures due to throttling. To avoid this throttling, create a managed snapshot from the vhd file and then use the managed snapshot to create multiple managed disks in short amount of time.
#Provide the subscription Id where Managed Disks will be created $subscriptionId = 'yourSubscriptionId' #Provide the name of your resource group where Managed Disks will be created. $resourceGroupName ='yourResourceGroupName' #Provide the name of the Managed Disk $diskName = 'yourDiskName' #Provide the size of the disks in GB. It should be greater than the VHD file size. $diskSize = '128' #Provide the storage type for Managed Disk. Premium_LRS or Standard_LRS. $storageType = 'Premium_LRS' #Provide the Azure region (e.g. westus) where Managed Disk will be located. #This location should be same as the storage account where VHD file is stored #Get all the Azure location using command below: #Get-AzLocation $location = 'westus' #Provide the URI of the VHD file (page blob) in a storage account. Please not that this is NOT the SAS URI of the storage container where VHD file is stored. #e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd #Note: VHD file can be deleted as soon as Managed Disk is created. $sourceVHDURI = 'https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd' #Provide the resource Id of the storage account where VHD file is stored. #e.g. /subscriptions/6472s1g8-h217-446b-b509-314e17e1efb0/resourceGroups/MDDemo/providers/Microsoft.Storage/storageAccounts/contosostorageaccount #This is an optional parameter if you are creating managed disk in the same subscription $storageAccountId = '/subscriptions/yourSubscriptionId/resourceGroups/yourResourceGroupName/providers/Microsoft.Storage/storageAccounts/yourStorageAccountName' #Set the context to the subscription Id where Managed Disk will be created Select-AzSubscription -SubscriptionId $SubscriptionId $diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName
This script uses following commands to create a managed disk from a VHD in different subscription. Each command in the table links to command specific documentation.
|New-AzDiskConfig||Creates disk configuration that is used for disk creation. It includes storage type, location, resource Id of the storage account where the parent VHD is stored, VHD URI of the parent VHD.|
|New-AzDisk||Creates a disk using disk configuration, disk name, and resource group name passed as parameters.|
For more information on the Azure PowerShell module, see Azure PowerShell documentation.
Additional virtual machine PowerShell script samples can be found in the Azure Windows VM documentation.