How to use Add-Azvhd to import Windows Operating System image to Microsoft Azure

To import a Windows VHD (Virtual Hard Disk) image to Azure using the Add-AzVhd command, you need to follow a sequence of steps. This involves preparing your VHD file, uploading it to an Azure Storage account, and then using the image or disk in Azure.

Here’s a detailed guide:

Prerequisites

  1. Azure PowerShell Module: Ensure you have the Azure PowerShell module installed. If you haven’t installed it yet, you can do so using:
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
  2. Azure Subscription: Make sure you are logged in to your Azure account using:
    Connect-AzAccount
  3. VHD Requirements: Ensure the VHD file meets these requirements:
    • It should be in the VHD format (not VHDX).
    • It should be fixed-size, not dynamic.
    • It should be Generalized using sysprep if you want to use it as an image for creating new VMs.

Step-by-Step Guide to Import a Windows Image using Add-AzVhd

  1. Prepare Variables

    Define variables for the storage account name, container name, VHD file path, and the blob destination URL. Replace the placeholders with your specific values:

    $storageAccountName = "yourStorageAccountName"
    $containerName = "yourContainerName"
    $localFilePath = "C:\Path\To\Your\VHD.vhd" # Path to your VHD file
    $vhdBlobName = "yourVHDName.vhd" # Name of the VHD in Azure Storage
  2. Get the Storage Account and Container Context

    Retrieve the storage account context to access your Azure storage account:

    $storageAccount = Get-AzStorageAccount -ResourceGroupName "yourResourceGroupName" -Name $storageAccountName
    $storageContext = $storageAccount.Context
  3. Upload the VHD using Add-AzVhd

    Use the Add-AzVhd command to upload the VHD file from your local machine to the Azure blob storage:

    Add-AzVhd -LocalFilePath $localFilePath -ResourceGroupName "yourResourceGroupName" `
    -Destination "https://$($storageAccountName).blob.core.windows.net/$containerName/$vhdBlobName" `
    -NumberOfUploaderThreads 32
    • LocalFilePath: The path to your VHD file on your local machine.
    • Destination: The full URL where the VHD will be stored in Azure.
    • NumberOfUploaderThreads: Controls the number of threads used for the upload process (default is 8, but using more can increase the upload speed).
  4. Verify the Upload

    After the upload is complete, you can verify that the VHD is in the Azure Storage container:

    Get-AzStorageBlob -Container $containerName -Context $storageContext
  5. Create a Managed Disk from the VHD (Optional)

    If you want to create a managed disk from this VHD, use the following command:

    $diskConfig = New-AzDiskConfig -AccountType Premium_LRS -Location "YourAzureRegion" `
    -CreateOption Import -SourceUri "https://$($storageAccountName).blob.core.windows.net/$containerName/$vhdBlobName"

    New-AzDisk -ResourceGroupName "yourResourceGroupName" -DiskName "yourDiskName" -Disk $diskConfig

    Replace “YourAzureRegion” with the region where you want to create the disk, like eastus, westus, etc.

  6. Create a VM from the Managed Disk (Optional)

    If you want to create a VM from the managed disk, follow the steps below:

    $vmConfig = New-AzVMConfig -VMName "YourVMName" -VMSize "Standard_DS1_v2" |
    Set-AzVMOperatingSystem -Windows -ComputerName "YourVMComputerName" -Credential (Get-Credential) |
    Set-AzVMSourceImage -Id $disk.Id

    New-AzVM -ResourceGroupName "yourResourceGroupName" -Location "YourAzureRegion" -VM $vmConfig

Summary

The Add-AzVhd command uploads a VHD file to Azure Storage, and then you can use it to create a managed disk or directly create a VM. This process is useful for migrating on-premises VM images to Azure or preparing custom VM images for use in your cloud environment.

Make sure to adjust the region, resource group, and other specific details as per your setup.

 

#Add-Azvhd #Azure #MicrosoftAzure

Leave a Comment

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

Scroll to Top