在相同或不同订阅中通过 PowerShell 复制托管磁盘Copy managed disks in the same subscription or different subscription with PowerShell

此脚本在相同或不同订阅中创建现有托管磁盘的副本。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.

必要时,请使用 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.

示例脚本Sample script

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

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

#Get the source managed disk
$managedDisk= Get-AzDisk -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-AzSubscription -SubscriptionId $targetSubscriptionId

$diskConfig = New-AzDiskConfig -SourceResourceId $managedDisk.Id -Location $managedDisk.Location -CreateOption Copy 

#Create a new managed disk in the target subscription and resource group
New-AzDisk -Disk $diskConfig -DiskName $managedDiskName -ResourceGroupName $targetResourceGroupName

脚本说明Script explanation

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

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

后续步骤Next steps

从托管磁盘创建虚拟机Create a virtual machine from a managed disk

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

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