Edit

Share via


Create a custom image from a VHD file by using Azure PowerShell

In this article, you learn how to create an Azure DevTest Labs virtual machine (VM) custom image from a virtual hard disk (VHD) file by using Azure PowerShell. You can also use the Azure portal to create a custom image.

In Azure DevTest Labs, you can use custom images to:

  • Create a VM from a VHD file that has all the software you need preinstalled.
  • Create VMs quickly, because you don't have to install all the required software on the target machines.
  • Clone a VM by creating a custom image from a VM, and then creating VMs based on that image.

Prerequisites

Create a custom image

The following Azure PowerShell steps create a DevTest Labs custom image from an uploaded VHD file by using a deployment template from the public DevTest Labs template repository.

  1. After you sign in to Azure, select the subscription you want to use by running Select-AzSubscription. Replace the <subscription ID> placeholder with your subscription ID.

    $subscriptionId = '<subscription ID>'
    Select-AzSubscription -SubscriptionId $subscriptionId
    
  2. Use Get-AzResource to get the lab object. Replace the <lab resource group name> and <lab name> placeholders with your own resource group and lab names, which you can get from the Azure portal.

    $labRg = '<lab resource group name>'
    $labName = '<lab name>'
    $lab = Get-AzResource -ResourceId ('/subscriptions/' + $subscriptionId + '/resourceGroups/' + $labRg + '/providers/Microsoft.DevTestLab/labs/' + $labName)
    
  3. Set up the parameters.

    • Replace the <custom image name> and <custom image description placeholders with a name and description for the custom image.
    • Replace the <VHD URI> placeholder with the URI of your uploaded VHD file. You can get the VHD file's URI from the Azure Storage container where you uploaded the file. An example VHD URI is: https://acontosolab1234.blob.core.windows.net/uploads/myvhd.vhd.
    $customImageName = '<custom image name>'
    $customImageDescription = '<custom image description>'
    $vhdUri = '<VHD URI>'
    
    $parameters = @{existingLabName="$($lab.Name)"; existingVhdUri=$vhdUri; imageOsType='windows'; isVhdSysPrepped=$false; imageName=$customImageName; imageDescription=$customImageDescription}
    
  4. Run New-AzResourceGroupDeployment to create the custom image by using a template according to the parameters.

    New-AzResourceGroupDeployment -ResourceGroupName $lab.ResourceGroupName -Name CreateCustomImage -TemplateUri 'https://raw.githubusercontent.com/Azure/azure-devtestlab/master/samples/DevTestLabs/QuickStartTemplates/201-dtl-create-customimage-from-vhd/azuredeploy.json' -TemplateParameterObject $parameters
    

Use a PowerShell script

You can combine the preceding steps to produce an Azure PowerShell script that creates a custom image from a VHD file. To use the script, replace the parameter values under the # Values to change comment with your own values.

# Values to change
$subscriptionId = '<Azure subscription ID>'
$labRg = '<Lab resource group name>'
$labName = '<Lab name>'
$vhdUri = '<VHD URI>'
$customImageName = '<Name for the custom image>'
$customImageDescription = '<Description for the custom image>'

# Select the desired Azure subscription.
Select-AzSubscription -SubscriptionId $subscriptionId

# Get the lab object.
$lab = Get-AzResource -ResourceId ('/subscriptions/' + $subscriptionId + '/resourceGroups/' + $labRg + '/providers/Microsoft.DevTestLab/labs/' + $labName)

# Set up the parameters object.
$parameters = @{existingLabName="$($lab.Name)"; existingVhdUri=$vhdUri; imageOsType='windows'; isVhdSysPrepped=$false; imageName=$customImageName; imageDescription=$customImageDescription}

# Create the custom image.
New-AzResourceGroupDeployment -ResourceGroupName $lab.ResourceGroupName -Name CreateCustomImage -TemplateUri 'https://raw.githubusercontent.com/Azure/azure-devtestlab/master/samples/DevTestLabs/QuickStartTemplates/201-dtl-create-customimage-from-vhd/azuredeploy.json' -TemplateParameterObject $parameters