在相同或不同订阅中通过 PowerShell 复制托管磁盘的快照Copy snapshot of a managed disk in same subscription or different subscription with PowerShell

此脚本会将托管磁盘的快照复制到相同或不同的订阅。This script copies a snapshot of a managed disk to same or different subscription. 将此脚本用于以下方案:Use this script for the following scenarios:

  1. 将高级存储 (Premium_LRS) 中的快照迁移到标准存储(Standard_LRS 或 Standard_ZRS)以降低成本。Migrate a snapshot in Premium storage (Premium_LRS) to Standard storage (Standard_LRS or Standard_ZRS) to reduce your cost.
  2. 将快照从本地冗余存储(Premium_LRS、Standard_LRS)迁移到区域冗余存储(Standard_ZRS),以从 ZRS 存储的更高可靠性中受益。Migrate a snapshot from locally redundant storage (Premium_LRS, Standard_LRS) to zone redundant storage (Standard_ZRS) to benefit from the higher reliability of ZRS storage.
  3. 将快照移到同一区域中的不同订阅,以延长保留时间。Move a snapshot to different subscription in the same region for longer retention.

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell 模块,然后运行 Connect-AzureRmAccount -Environment AzureChinaCloud 创建与 Azure 的连接。If needed, install the Azure PowerShell module using the instructions found in the Azure PowerShell guide, and then run Connect-AzureRmAccount -Environment AzureChinaCloud to create a connection with Azure. 此外,用户配置文件的 .ssh 目录中需具备名为 id_rsa.pub 的 SSH 公钥。Also, you need to have an SSH public key named id_rsa.pub in the .ssh directory of your user profile.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

示例脚本Sample script

# Sign-in the Azure China Cloud
Connect-AzAccount -Environment AzureChinaCloud

#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-AzSubscription -SubscriptionId $sourceSubscriptionId

#Get the source snapshot
$snapshot= Get-AzSnapshot -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-AzSubscription -SubscriptionId $targetSubscriptionId

#We recommend you to store your snapshots in Standard storage to reduce cost. Please use Standard_ZRS in regions where zone redundant storage (ZRS) is available, otherwise use Standard_LRS
#Please check out the availability of ZRS here: https://docs.microsoft.com/Az.Storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
$snapshotConfig = New-AzSnapshotConfig -SourceResourceId $snapshot.Id -Location $snapshot.Location -CreateOption Copy -SkuName Standard_LRS

#Create a new snapshot in the target subscription and resource group
New-AzSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotName -ResourceGroupName $targetResourceGroupName 

脚本说明Script explanation

此脚本使用以下命令,通过源快照的 ID 在目标订阅中创建快照。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.

命令Command 注释Notes
New-AzSnapshotConfigNew-AzSnapshotConfig 创建用于创建快照的快照配置。Creates snapshot configuration that is used for snapshot creation. 包括父快照的资源 ID 以及与父快照相同的位置。It includes the resource Id of the parent snapshot and location that is same as the parent snapshot.
New-AzSnapshotNew-AzSnapshot 使用快照配置、快照名称和作为参数传递的资源组名称创建快照。Creates a snapshot using snapshot configuration, snapshot name, and resource group name passed as parameters.

后续步骤Next steps

从快照创建虚拟机Create a virtual machine from a snapshot

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 PowerShell 脚本示例。Additional virtual machine PowerShell script samples can be found in the Azure Linux VM documentation.