Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
Owner or Contributor permissions in an existing lab.
Azure PowerShell installed. You can use Azure Cloud Shell or install PowerShell locally.
- In Cloud Shell, select the PowerShell environment.
- For a local PowerShell installation, run
Update-Module -Name Az
to get the latest version of Azure PowerShell, and run Connect-AzAccount to sign in to Azure.
A VHD file uploaded to the Azure Storage account for the lab. To upload a VHD file:
- Go to your lab storage account in the Azure portal and select Upload.
- Browse to and select the VHD file, select the uploads container or create a new container named uploads for the file, and then select Upload.
You can also upload a VHD file by following the instructions in any of these articles:
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.
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
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)
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}
- Replace the
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