Create a VM from a VHD

While there are many ways to migrate Hyper-V guests using Azure Migrate: Server Migration and Azure Site Recovery Provider, sometimes you might want to simply create an Azure VM using an existing VHD that you have uploaded to an Azure Storage account.

If you have an existing managed disk from a VM that was deleted, you can also create a new VM from it.
When creating a VM from a Storage account, the disk remains as an unmanaged disk. For production use, it is recommended to convert it to a managed disk, providing the following advantages, including:

For testing purposes, you can leave the disk as unmanaged. The only benefit to using an unmanaged disk is mostly cost; with Azure Storage accounts, you only pay for what you use. For example, if you upload 1GB of data, you are only charged for this size, excluding additional costs, including ingress and egress of data relating to the type of Storage account you have.

Managed disks on the other hand, you pay for the size of the VHD you specify; if the disk size is 30GB but you are only utilizing 15GB of it, you are still billed for 30GB because managed disks are fixed sized, not dynamically allocated or thinly provisioned. 

Also, when uploading a VHD to a Storage account, be sure to upload as a page-blob, otherwise you won't be able to use the VHD when creating the VM. The PowerShell script below focuses on creating a VM from an existing VHD that’s in a Storage account:


Clear-Host
$vmName = "MyVM"
$rgName = "Servers"
$subnetName = "servers-subnet1"
$location = "East US"
$vnetName = "magrin"
$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
$nicName = $vmName
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[2].Id
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_B4ms"
$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
$osDiskUri = https://mystorageaccount.blob.core.windows.net/vhds/mail_OsDisk.vhd
$osDiskName = $vmName + "_OsDisk"
$vm = Set-AzVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption Attach -Windows
New-AzVM -ResourceGroupName $rgName -Location $location -VM $vm