Copy snapshot of a managed disk in same subscription or different subscription with PowerShell
This script creates a copy of a snapshot in the same same subscription or different subscription. Use this script to move a snapshot to different subscription for data retention. Storing snapshots in different subscription protect you from accidental deletion of snapshots in your main subscription.
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 snapshot exists $sourceSubscriptionId='yourSourceSubscriptionId' #Provide the name of your resource group where snapshot exists $sourceResourceGroupName='yourResourceGroupName' #Provide the name of the snapshot $snapshotName='yourSnapshotName' #Set the context to the subscription Id where snapshot exists Select-AzureRmSubscription -SubscriptionId $sourceSubscriptionId #Get the source snapshot $snapshot= Get-AzureRmSnapshot -ResourceGroupName $sourceResourceGroupName -Name $snapshotName #Provide the subscription Id of the subscription where snapshot will be copied to #If snapshot 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='yourTargetResourceGroupName' #Set the context to the subscription Id where snapshot will be copied to #If snapshot is copied to the same subscription then you can skip this step Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId $snapshotConfig = New-AzureRmSnapshotConfig -SourceResourceId $snapshot.Id -Location $snapshot.Location -CreateOption Copy #Create a new snapshot in the target subscription and resource group New-AzureRmSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotName -ResourceGroupName $targetResourceGroupName
This script uses following commands to create a snapshot in the target subscription using the Id of the source snapshot. Each command in the table links to command specific documentation.
|New-AzureRmSnapshotConfig||Creates snapshot configuration that is used for snapshot creation. It includes the resource Id of the parent snapshot and location that is same as the parent snapshot.|
|New-AzureRmSnapshot||Creates a snapshot using snapshot configuration, snapshot 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.