使用 PowerShell 将托管磁盘的 VHD 导出/复制到不同区域中的存储帐户Export/Copy the VHD of a managed disk to a storage account in different region with PowerShell

此脚本将托管磁盘的 VHD 导出到不同区域中的存储帐户。This script exports the VHD of a managed disk to a storage account in different region. 它首先生成托管磁盘的 SAS URI,然后使用该 SAS URI 将基础 VHD 复制到不同区域中的存储帐户。It first generates the SAS URI of the managed disk and then uses it to copy the underlying VHD to a storage account in different region. 使用此脚本将托管磁盘复制到另一区域以进行区域扩展。Use this script to copy managed disks to another region for regional expansion.

必要时,请使用 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 managed disk is created
$subscriptionId = "yourSubscriptionId"

#Provide the name of your resource group where managed is created
$resourceGroupName ="yourResourceGroupName"

#Provide the managed disk name 
$diskName = "yourDiskName"

#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://docs.azure.cn/storage/storage-dotnet-shared-access-signature-part-1
$sasExpiryDuration = "3600"

#Provide storage account name where you want to copy the underlying VHD of the managed disk. 
$storageAccountName = "yourstorageaccountName"

#Name of the storage container where the downloaded VHD will be stored
$storageContainerName = "yourstoragecontainername"

#Provide the key of the storage account where you want to copy the VHD of the managed disk. 
$storageAccountKey = 'yourStorageAccountKey'

#Provide the name of the destination VHD file to which the VHD of the managed disk will be copied.
$destinationVHDFileName = "yourvhdfilename"

#Set the value to 1 to use AzCopy tool to download the data. This is the recommended option for faster copy.
#Download AzCopy v10 from the link here: https://docs.azure.cn/storage/common/storage-use-azcopy-v10
#Ensure that AzCopy is downloaded in the same folder as this file
#If you set the value to 0 then Start-AzStorageBlobCopy will be used. Azure storage will asynchronously copy the data. 
$useAzCopy = 1 

# Set the context to the subscription Id where managed disk is created
Select-AzSubscription -SubscriptionId $SubscriptionId

#Generate the SAS for the managed disk 
$sas = Grant-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $diskName -DurationInSecond $sasExpiryDuration -Access Read 

#Create the context of the storage account where the underlying VHD of the managed disk will be copied
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

#Copy the VHD of the managed disk to the storage account
if($useAzCopy -eq 1)
{
    $containerSASURI = New-AzStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds($sasExpiryDuration) -FullUri -Name $storageContainerName -Permission rw
    .\azcopy copy $sas.AccessSAS $containerSASURI

}else{

    Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName
}

脚本说明Script explanation

此脚本使用以下命令生成托管磁盘的 SAS URI,然后使用该 SAS URI 将基础 VHD 复制到存储帐户。This script uses the following commands to generate SAS URI of a managed disk and copies the underlying VHD to a storage account using the SAS URI. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to the command specific documentation.

命令Command 注释Notes
Grant-AzDiskAccessGrant-AzDiskAccess 为托管磁盘生成 SAS URI,该 SAS URI 用于将基础 VHD 复制到存储帐户。Generates SAS URI for a managed disk that is used to copy the underlying VHD to a storage account.
New-AzStorageContextNew-AzStorageContext 使用帐户名和密钥创建存储帐户上下文。Creates a storage account context using the account name and key. 此上下文可用于对存储帐户执行读/写操作。This context can be used to perform read/write operations on the storage account.
Start-AzStorageBlobCopyStart-AzStorageBlobCopy 将快照的基础 VHD 复制到存储帐户Copies the underlying VHD of a snapshot to a storage account

后续步骤Next steps

从 VHD 创建托管磁盘Create a managed disk from a VHD

从托管磁盘创建虚拟机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.