通过将现有托管 OS 磁盘与 PowerShell 配合使用来创建虚拟机Create a virtual machine using an existing managed OS disk with PowerShell

此脚本通过将现有托管磁盘附加为 OS 磁盘来创建虚拟机。This script creates a virtual machine by attaching an existing managed disk as OS disk. 在前面的方案中使用此脚本:Use this script in preceding scenarios:

  • 基于从不同订阅中的托管磁盘复制的现有托管 OS 磁盘创建 VMCreate a VM from an existing managed OS disk that was copied from a managed disk in different subscription
  • 基于从专用 VHD 文件创建的现有托管磁盘创建 VMCreate a VM from an existing managed disk that was created from a specialized VHD file
  • 基于从快照创建的现有托管 OS 磁盘创建 VMCreate a VM from an existing managed OS disk that was created from a snapshot

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

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

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

#Provide the name of the OS disk that will be created using the snapshot
$osDiskName = 'yourOSDiskName'

#Provide the name of an existing virtual network where virtual machine will be created
$virtualNetworkName = 'yourVNETName'

#Provide the name of the virtual machine
$virtualMachineName = 'yourVMName'

#Provide the size of the virtual machine
#e.g. Standard_DS3
#Get all the vm sizes in a region using below script:
#e.g. Get-AzVMSize -Location chinanorth
$virtualMachineSize = 'Standard_DS3'

#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 -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy

$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName

#Initialize virtual machine configuration
$VirtualMachine = New-AzVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize

#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows

#Create a public IP for the VM
$publicIp = New-AzPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic

#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName

# Create NIC in the first subnet of the virtual network
$nic = New-AzNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id

$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $nic.Id

#Create the virtual machine with Managed Disk
New-AzVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令获取托管磁盘属性,将托管磁盘附加到新 VM 并创建 VM。This script uses the following commands to get managed disk properties, attach a managed disk to a new VM and create a VM. 表中的每一项均链接到特定于命令的文档。Each item in the table links to command specific documentation.

命令Command 注释Notes
Get-AzDiskGet-AzDisk 基于磁盘的名称和资源组获取磁盘对象。Gets disk object based on the name and the resource group of a disk. 返回的磁盘对象的 ID 属性用于将磁盘附加到新 VMId property of the returned disk object is used to attach the disk to a new VM
New-AzVMConfigNew-AzVMConfig 创建 VM 配置。Creates a VM configuration. 此配置包括 VM 名称、操作系统和管理凭据等信息。This configuration includes information such as VM name, operating system, and administrative credentials. 在创建 VM 期间使用此配置。The configuration is used during VM creation.
Set-AzVMOSDiskSet-AzVMOSDisk 使用磁盘的 ID 属性将托管磁盘作为 OS 磁盘附加到新虚拟机Attaches a managed disk using the Id property of the disk as OS disk to a new virtual machine
New-AzPublicIpAddressNew-AzPublicIpAddress 创建公共 IP 地址。Creates a public IP address.
New-AzNetworkInterfaceNew-AzNetworkInterface 创建网络接口。Creates a network interface.
New-AzVMNew-AzVM 创建虚拟机。Create a virtual machine.
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组及其中包含的所有资源。Removes a resource group and all resources contained within.

对于市场映像,使用 Set-AzVMPlan 设置计划信息。For marketplace images use Set-AzVMPlan to set the plan information.

Set-AzVMPlan -VM $VirtualMachine -Publisher $Publisher -Product $Product -Name $Bame

后续步骤Next steps

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

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