使用 PowerShell 从快照创建托管磁盘Create a managed disk from a snapshot with PowerShell

此脚本从快照创建托管磁盘。This script creates a managed disk from a snapshot. 使用它从 OS 和数据磁盘的快照还原虚拟机。Use it to restore a virtual machine from snapshots of OS and data disks. 从各自的快照创建 OS 和数据托管磁盘,然后通过附加托管磁盘创建新的虚拟机。Create OS and data managed disks from respective snapshots and then create a new virtual machine by attaching managed disks. 还可以通过附加从快照创建的数据磁盘还原现有 VM 的数据磁盘。You can also restore data disks of an existing VM by attaching data disks created from snapshots.

如果没有 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
$subscriptionId = 'yourSubscriptionId'

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

#Provide the name of the snapshot that will be used to create Managed Disks
$snapshotName = 'yourSnapshotName'

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

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

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

#Provide the Azure region (e.g. chinanorth) where Managed Disks will be located.
#This location should be same as the snapshot location
#Get all the Azure location using command below:
#Get-AzLocation
$location = 'chinanorth'

#Set the context to the subscription Id where Managed Disk will be created
Select-AzSubscription -SubscriptionId $SubscriptionId

$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 

$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id

New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

脚本说明Script explanation

此脚本使用以下命令从快照创建托管磁盘。This script uses following commands to create a managed disk from a snapshot. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
Get-AzSnapshotGet-AzSnapshot 获取快照属性。Gets snapshot properties.
New-AzDiskConfigNew-AzDiskConfig 创建用于磁盘创建的磁盘配置。Creates disk configuration that is used for disk creation. 包括父快照的资源 ID、与父快照位置相同的位置以及存储类型。It includes the resource Id of the parent snapshot, location that is same as the location of parent snapshot and the storage type.
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.