This script creates a copy of an existing managed disk in the same subscription or different subscription. The new disk is created in the same region as the parent managed disk.
If needed, install the Azure PowerShell module using the instructions found in the Azure PowerShell guide, and then run
Login-AzureRmAccount to create a connection with Azure. Also, you need to have an SSH public key named
id_rsa.pub in the .ssh directory of your user profile.
If you don't have an Azure subscription, create a free account before you begin.
#Provide the subscription Id of the subscription where managed disk exists $sourceSubscriptionId='yourSourceSubscriptionId' #Provide the name of your resource group where managed disk exists $sourceResourceGroupName='mySourceResourceGroupName' #Provide the name of the managed disk $managedDiskName='myDiskName' #Set the context to the subscription Id where Managed Disk exists Select-AzureRmSubscription -SubscriptionId $sourceSubscriptionId #Get the source managed disk $managedDisk= Get-AzureRMDisk -ResourceGroupName $sourceResourceGroupName -DiskName $managedDiskName #Provide the subscription Id of the subscription where managed disk will be copied to #If managed disk is copied to the same subscription then you can skip this step $targetSubscriptionId='yourTargetSubscriptionId' #Name of the resource group where snapshot will be copied to $targetResourceGroupName='myTargetResourceGroupName' #Set the context to the subscription Id where managed disk will be copied to #If snapshot is copied to the same subscription then you can skip this step Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId $diskConfig = New-AzureRmDiskConfig -SourceResourceId $managedDisk.Id -Location $managedDisk.Location -CreateOption Copy #Create a new managed disk in the target subscription and resource group New-AzureRmDisk -Disk $diskConfig -DiskName $managedDiskName -ResourceGroupName $targetResourceGroupName
This script uses following commands to create a new managed disk in the target subscription using the Id of the source managed disk. Each command in the table links to command specific documentation.
|New-AzureRmDiskConfig||Creates disk configuration that is used for disk creation. It includes the resource Id of the parent disk and location that is same as the location of parent disk.|
|New-AzureRmDisk||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 Windows VM documentation.