使用 PowerShell 在相同或不同订阅的存储帐户中从 VHD 文件创建托管磁盘

此脚本在相同或不同订阅的存储帐户中从 VHD 文件创建托管磁盘。 使用此脚本将专用(未经过通用化/sysprep 处理)的 VHD 导入到托管 OS 磁盘以创建虚拟机。 同时,使用它将数据 VHD 导入到托管数据磁盘。

请勿在短时间内从一个 VHD 文件创建多个相同的托管磁盘。 若要从 VHD 文件创建托管磁盘,需创建该 VHD 文件的 blob 快照,然后再使用快照创建托管磁盘。 一分钟内只可创建一个 blob 快照,此限制会导致磁盘创建失败。 若要避免此限制,请从 VHD 文件创建托管快照,然后使用托管快照在短时间内创建多个托管磁盘。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

示例脚本


<#

.DESCRIPTION

This sample demonstrates how to create a Managed Disk from a VHD file. 
Create Managed Disks from VHD files in following scenarios:
1. Create a Managed OS Disk from a specialized VHD file. A specialized VHD is a copy of VHD from an exisitng VM that maintains the user accounts, applications and other state data from your original VM. 
   Attach this Managed Disk as OS disk to create a new virtual machine.
2. Create a Managed data Disk from a VHD file. Attach the Managed Disk to an existing VM or attach it as data disk to create a new virtual machine.

.NOTES

1. Before you use this sample, please install the latest version of Azure PowerShell from here: http://go.microsoft.com/?linkid=9811175&clcid=0x409
2. Provide the appropriate values for each variable. Note: The angled brackets should not be included in the values you provide.


#>

#Provide the subscription Id
$subscriptionId = 'yourSubscriptionId'

#Provide the name of your resource group
$resourceGroupName ='yourResourceGroupName'

#Provide the name of the Managed Disk
$diskName = 'yourDiskName'

#Provide the size of the disks in GB. It should be greater than the VHD file size.
$diskSize = '128'

#Provide the URI of the VHD file that will be used to create Managed Disk. 
# VHD file can be deleted as soon as Managed Disk is created.
# e.g. https://contosostorageaccount1.blob.core.chinacloudapi.cn/vhds/contoso-um-vm120170302230408.vhd 
$vhdUri = 'https://contosoststorageaccount1.blob.core.chinacloudapi.cn/vhds/contosovhd123.vhd' 

#Provide the resource Id of the storage account where VHD file is stored.
#e.g. /subscriptions/6472s1g8-h217-446b-b509-314e17e1efb0/resourceGroups/MDDemo/providers/Microsoft.Storage/storageAccounts/contosostorageaccount
$storageAccountId = '/subscriptions/yourSubscriptionId/resourceGroups/yourResourceGroupName/providers/Microsoft.Storage/storageAccounts/yourStorageAccountName'

#Provide the storage type for the Managed Disk. PremiumLRS or StandardLRS.
$sku = 'Premium_LRS'

#Provide the Azure location (e.g. chinanorth) where Managed Disk will be located. 
#The location should be same as the location of the storage account where VHD file is stored.
#Get all the Azure location using command below:
#Get-AzureRmLocation
$location = 'chinanorth'

#Set the context to the subscription Id where Managed Disk will be created
Set-AzContext -Subscription $subscriptionId

#If you're creating an OS disk, add the following lines
#Acceptable values are either Windows or Linux
#$OSType = 'yourOSType'
#Acceptable values are either V1 or V2
#$HyperVGeneration = 'yourHyperVGen'

#If you're creating an OS disk, add -HyperVGeneration and -OSType parameters
$diskConfig = New-AzDiskConfig -SkuName $sku -Location $location -DiskSizeGB $diskSize -SourceUri $vhdUri -StorageAccountId $storageAccountId -CreateOption Import

#Create Managed disk
New-AzDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $resourceGroupName

脚本说明

此脚本使用以下命令在不同订阅中从 VHD 创建托管磁盘。 表中的每条命令均链接到特定于命令的文档。

命令 注释
New-AzDiskConfig 创建用于磁盘创建的磁盘配置。 它包括存储父 VHD 的存储帐户的存储类型、位置和资源 ID,以及父 VHD 的 VHD URI。
New-AzDisk 使用磁盘配置、磁盘名称和作为参数传递的资源组名称创建磁盘。

后续步骤

通过将托管磁盘附加为 OS 磁盘来创建虚拟机

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档

可以在 Azure Windows VM 文档中找到其他虚拟机 PowerShell 脚本示例。