使用 PowerShell 将托管磁盘的快照复制到同一订阅或不同订阅 (Windows)
此脚本会将托管磁盘的快照复制到相同或不同的订阅。 将此脚本用于以下方案:
- 将高级存储 (Premium_LRS) 中的快照迁移到标准存储(Standard_LRS 或 Standard_ZRS)以降低成本。
- 将快照从本地冗余存储(Premium_LRS、Standard_LRS)迁移到区域冗余存储(Standard_ZRS),以从 ZRS 存储的更高可靠性中受益。
- 将快照移到同一区域中的不同订阅,以延长保留时间。
必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell 模块,然后运行 Connect-AzAccount -Environment AzureChinaCloud
创建与 Azure 的连接。 此外,用户配置文件的 .ssh 目录中需具备名为 id_rsa.pub
的 SSH 公钥。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
示例脚本
#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://learn.microsoft.com/en-us/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
脚本说明
此脚本使用以下命令,通过源快照的 ID 在目标订阅中创建快照。 表中的每条命令均链接到特定于命令的文档。
命令 | 注释 |
---|---|
New-AzSnapshotConfig | 创建用于创建快照的快照配置。 包括父快照的资源 ID 以及与父快照相同的位置。 |
New-AzSnapshot | 使用快照配置、快照名称和作为参数传递的资源组名称创建快照。 |
后续步骤
有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档。
可以在 Azure Windows VM 文档中找到其他虚拟机 PowerShell 脚本示例。