Create a snapshot from a VHD to create multiple identical managed disks in small amount of time with PowerShell
This script creates a snapshot from a VHD file in a storage account in same or different subscription. Use this script to import a specialized (not generalized/sysprepped) VHD to a snapshot and then use the snapshot to create multiple identical managed disks in small amount of time. Also, use it to import a data VHD to a snapshot and then use the snapshot to create multiple managed disks in small amount of time.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either
PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.
To launch Azure Cloud Shell:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the top-right menu bar in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
- Launch Cloud Shell.
- Select the Copy button on a code block to copy the code.
- Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
- Press Enter to run the code.
This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.
#Provide the subscription Id where snapshot will be created $subscriptionId = 'yourSubscriptionId' #Provide the name of your resource group where snapshot will be created. $resourceGroupName ='yourResourceGroupName' #Provide the name of the snapshot $snapshotName = 'yourSnapshotName' #Provide the storage type for snapshot. PremiumLRS or StandardLRS. $storageType = 'StandardLRS' #Provide the Azure region (e.g. westus) where snapshot will be located. #This location should be same as the storage account location where VHD file is stored #Get all the Azure location using command below: #Get-AzLocation $location = 'westus' #Provide the URI of the VHD file (page blob) in a storage account. Please not that this is NOT the SAS URI of the storage container where VHD file is stored. #e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd #Note: VHD file can be deleted as soon as Managed Disk is created. $sourceVHDURI = 'https://yourStorageAccountName.blob.core.windows.net/vhds/yourVHDName.vhd' #Provide the resource Id of the storage account where VHD file is stored. #e.g. /subscriptions/6582b1g7-e212-446b-b509-314e17e1efb0/resourceGroups/MDDemo/providers/Microsoft.Storage/storageAccounts/contosostorageaccount1 #This is an optional parameter if you are creating snapshot in the same subscription $storageAccountId = '/subscriptions/yourSubscriptionId/resourceGroups/yourResourceGroupName/providers/Microsoft.Storage/storageAccounts/yourStorageAccountName' #Set the context to the subscription Id where Managed Disk will be created Select-AzSubscription -SubscriptionId $SubscriptionId $snapshotConfig = New-AzSnapshotConfig -AccountType $storageType -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI Â New-AzSnapshot -Snapshot $snapshotConfig -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName Â
This script uses following commands to create a managed disk from a VHD in different subscription. Each command in the table links to command specific documentation.
|New-AzDiskConfig||Creates disk configuration that is used for disk creation. It includes storage type, location, resource Id of the storage account where the parent VHD is stored, and VHD URI of the parent VHD.|
|New-AzDisk||Creates a disk using disk configuration, disk name, and resource group name passed as parameters.|
For more information on the Azure PowerShell module, see Azure PowerShell documentation.
Additional virtual machine PowerShell script samples can be found in the Azure Linux VM documentation.