Upload a VHD to Azure and create a virtual machine

Scenario

You need to create a virtual machine from an existing device and host it in Azure.

Requirements

It is assumed in this post that you already have an Azure tenant and that you have a subscription which can be used to upload the VHD and create a virtual machine.

This post is a compilation of information from many sources which I will reference throughout. I am putting this together in the hopes of making this process ‘easier’ to accomplish.

Ready, Set, Go!

Step – 1

Turn off Bitlocker on the device

  • Backup the device recovery key from Azure (if previously joined) or from the local device

Step – 2

Create a VHDX file using Hyper-V manager (you will need an External Drive for this step)

  • From the device you need to convert, install Hyper-V manager tools with Powershell
DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
  • Open Hyper-V manager and create a ‘new’ virtual disk from the devices physical disk

Step – 3

Create a virtual machine in Hyper-V manager

Choose Options:

  • Gen 2
  • Set memory and disable ‘dynamic’
  • Choose ‘default switch’ for network connection
  • Choose ‘use an existing virtual hard disk’ and select the disk created in Step – 2

Step – 4

Prepare the VM for Azure AD

Reference:  Prepare a Windows VHD to upload to Azure – Azure Virtual Machines | Microsoft Docs

  • Set Coordinated Universal Time (UTC) time for Windows
  • Set the power profile to high performance
  • Enable Remote Desktop services with network level authentication
  • Set FW rules
  • Restart VM and ensure you can connect via Hyper-V with no bootloader issues
    • Ensure you can connect to it via RDP
  • Install the Azure Agent from an elevated command prompt : HERE
msiexec.exe /i c:\tmp\WindowsAzureVmAgent.2.7.41491.949_191001-1418.fre.msi /L*v c:\tmp\msiexec.log  
  • Restart the VM and connect with RDP
    • Verify it boots ‘ok’ from Hyper-V manager
    • Verify login ‘ok’
    • Verify Azure VM agent installed
  • Shut the VM down and delete it
  • Remove the .avhdx file from the external drive if present and restart the physical device

Step – 5

Convert the .VHDX file to VHD using PowerShell

Convert-VHD -path D:\DiskName.vhdx -DestinationPath D:\DiskName.vhd

Step – 6

Resize the VHD to 256G (or more depending on the size required)

Resize-VHD -Path 'PathtoYourFixedSized.VHD' -SizeBytes '274877906944'

Sizes accepted in Azure (example): https://github.com/MicrosoftDocs/azure-docs/issues/48403

  • 128 = 137438953472
  • 256 = 274877906944

Step – 7

Create managed disk in Azure

Reference: Upload a VHD to Azure or copy a disk across regions – Azure PowerShell – Azure Virtual Machines | Microsoft Docs

#Connect to Azure AZ
Connect-AzAccount
#Set variables
$DiskName = 'DiskName'
$ResourceGroupName = 'ResourceGroupName'
#Set the correct subscription where the resource group is if you havemultiple subscriptions
Get-azsubscription
Set-AzContext -SubscriptionID "944abcd7-1e40-41f6-b357-cfe980f26543"
#Use the file size amount from Get-VHD -Path 'PathtoYourFixedSized.VHD' | Select-Object * to create your disk in Azure
$diskconfig = New-AzDiskConfig -AccountType Standard_LRS -Location 'Canada Central' -UploadSizeInBytes '274877907456' -OsType Windows -HyperVGeneration "V2" -CreateOption Upload
New-AzDisk -ResourceGroupName $ResourceGroupName -DiskName $DiskName -Disk $diskconfig

Status of new disk will show ‘Ready to Upload’ once created

#Generate SAS URL for the Azure empty disk
$diskSas = Grant-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $DiskName -DurationInSecond 86400 -Access Write
$disk = Get-AzDisk -ResourceGroupName $ResourceGroupName -DiskName $DiskName

Status will change to ‘Active Upload’ once SAS URL is generated

Step – 8

Download AzCopy : Copy or move data to Azure Storage by using AzCopy v10 | Microsoft Docs

Step – 9

Upload the VHD to the Azure managed disk using Azcopy

  • Add Azcopy to environment PATH
$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")
[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\TOP\AZCopy\", "User")
  • Login and execute the disk copy to Azure AD managed disk
Azcopy login
azcopy c "Path to .vhd" $diskSas.AccessSAS --blob-type PageBlob

Wait until the job completes and you see the Final Job Status of Completed

Step – 10

Confirm you see the disk in Azure AD by searching for ‘Disks’

Disk will display as ‘unattached’ and ‘Create VM’ will be greyed out until you ‘revoke disk access’

#Revoke disk access once completed
Revoke-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $DiskName

Step – 11

Create VM from managed disk

Step – 12

Once the VM has been created access the resource and perform post configuration steps

  • Verify the expected settings are present for Azure Agent once VM has been provisioned

Done!

From here you could continue to perform such tasks as:

  • Configure VM backup
  • Test RDP
  • Configure Alert notifications
  • Set scheduled start/stop times

Hope this post helped make the process easier to accomplish… cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *