使用 PowerShell 从专用磁盘创建 Windows VMCreate a Windows VM from a specialized disk by using PowerShell

通过将专用托管磁盘附加为 OS 磁盘来创建新 VM。Create a new VM by attaching a specialized managed disk as the OS disk. 专用磁盘是包含原始 VM 中的用户帐户、应用程序和其他状态数据的现有 VM 中虚拟硬盘 (VHD) 的副本。A specialized disk is a copy of a virtual hard disk (VHD) from an existing VM that contains the user accounts, applications, and other state data from your original VM.

使用专用 VHD 创建新 VM 时,新 VM 将保留原始 VM 的计算机名。When you use a specialized VHD to create a new VM, the new VM retains the computer name of the original VM. 还会保留其他计算机特定信息,在某些情况下,这种重复信息可能会导致问题。Other computer-specific information is also kept and, in some cases, this duplicate information could cause issues. 复制 VM 时,请注意应用程序依赖哪些类型的计算机特定信息。When copying a VM, be aware of what types of computer-specific information your applications rely on.

有几种选项:You have several options:

还可以使用 Azure 门户从专用 VHD 创建新 VMYou can also use the Azure portal to create a new VM from a specialized VHD.

本文介绍如何使用托管磁盘。This article shows you how to use managed disks. 如果有需要使用存储帐户的旧版部署,请参阅从存储帐户中的专用 VHD 创建 VMIf you have a legacy deployment that requires using a storage account, see Create a VM from a specialized VHD in a storage account.

我们建议你将单个 VHD 或快照的并发部署数限制为 20 个 VM。We recommend that you limit the number of concurrent deployments to 20 VMs from a single VHD or snapshot.

选项 1:使用现有磁盘Option 1: Use an existing disk

如果你的 VM 已删除,并且你希望重复使用 OS 磁盘创建新 VM,请使用 Get-AzDiskIf you had a VM that you deleted and you want to reuse the OS disk to create a new VM, use Get-AzDisk.

Connect-AzAccount -Environment AzureChinaCloud

$resourceGroupName = 'myResourceGroup'
$osDiskName = 'myOsDisk'
$osDisk = Get-AzDisk `
-ResourceGroupName $resourceGroupName `
-DiskName $osDiskName

现在可以将此磁盘作为 OS 磁盘附加到新 VMYou can now attach this disk as the OS disk to a new VM.

选项 2:上传专用 VHDOption 2: Upload a specialized VHD

可从使用本地虚拟化工具(如 Hyper-V)创建的专用 VM 或从另一个云导出的 VM 上传 VHD。You can upload the VHD from a specialized VM created with an on-premises virtualization tool, like Hyper-V, or a VM exported from another cloud.

准备 VMPrepare the VM

使用原始 VHD 创建新的 VM。Use the VHD as-is to create a new VM.

  • 准备好要上传到 Azure 的 Windows VHDPrepare a Windows VHD to upload to Azure. 不要使用 Sysprep 通用化 VM。Do not generalize the VM by using Sysprep.
  • 删除 VM 上安装的所有来宾虚拟化工具和代理(例如 VMware 工具)。Remove any guest virtualization tools and agents that are installed on the VM (such as VMware tools).
  • 确保 VM 配置为从 DHCP 获取 IP 地址和 DNS 设置。Make sure the VM is configured to get the IP address and DNS settings from DHCP. 这可以确保服务器在启动时获得虚拟网络中的 IP 地址。This ensures that the server obtains an IP address within the virtual network when it starts up.

上传 VHDUpload the VHD

现在可以直接将 VHD 上传到托管磁盘中。You can now upload a VHD straight into a managed disk. 有关说明,请参阅使用 Azure PowerShell 将 VHD 上传到 AzureFor instructions, see Upload a VHD to Azure using Azure PowerShell.

选项 3:复制现有的 Azure VMOption 3: Copy an existing Azure VM

通过创建 VM 快照来创建使用托管磁盘的 VM 副本,然后使用该快照创建一个新的托管磁盘和一个新 VM。You can create a copy of a VM that uses managed disks by taking a snapshot of the VM, and then by using that snapshot to create a new managed disk and a new VM.

如果要将现有 VM 复制到其他区域,可能需要使用 azcopy 在其他区域中创建磁盘副本If you want to copy an existing VM to another region, you might want to use azcopy to create a copy of a disk in another region.

拍摄 OS 磁盘快照Take a snapshot of the OS disk

可创建整个 VM(包括所有磁盘)的快照或仅创建单个磁盘的快照。You can take a snapshot of an entire VM (including all disks) or of just a single disk. 以下步骤说明了如何使用 New-AzSnapshot cmdlet 仅创建 VM OS 磁盘的快照。The following steps show you how to take a snapshot of just the OS disk of your VM with the New-AzSnapshot cmdlet.

首先设置一些参数。First, set some parameters.

$resourceGroupName = 'myResourceGroup' 
$vmName = 'myVM'
$location = 'chinanorth' 
$snapshotName = 'mySnapshot'  

获取 VM 对象。Get the VM object.

$vm = Get-AzVM -Name $vmName `
   -ResourceGroupName $resourceGroupName

获取 OS 磁盘名称。Get the OS disk name.

$disk = Get-AzDisk -ResourceGroupName $resourceGroupName `
  -DiskName $vm.StorageProfile.OsDisk.Name

创建快照配置。Create the snapshot configuration.

$snapshotConfig =  New-AzSnapshotConfig `
  -SourceUri $disk.Id `
  -OsType Windows `
  -CreateOption Copy `
  -Location $location 

创建快照。Take the snapshot.

$snapShot = New-AzSnapshot `
   -Snapshot $snapshotConfig `
   -SnapshotName $snapshotName `
   -ResourceGroupName $resourceGroupName

若要使用快照创建高性能的 VM,请将 -AccountType Premium_LRS 参数添加到 New-AzSnapshotConfig 命令。To use this snapshot to create a VM that needs to be high-performing, add the parameter -AccountType Premium_LRS to the New-AzSnapshotConfig command. 此参数将创建快照,以便将其存储为高级托管磁盘。This parameter creates the snapshot so that it's stored as a Premium Managed Disk. 高级托管磁盘的费用比标准托管磁盘更高,因此,在使用该参数之前,请确认需要高级托管磁盘。Premium Managed Disks are more expensive than Standard, so be sure you'll need Premium before using this parameter.

从快照创建新磁盘Create a new disk from the snapshot

使用 New-AzDisk 基于快照创建托管磁盘。Create a managed disk from the snapshot by using New-AzDisk. 此示例使用“myOSDisk”作为磁盘名称。This example uses myOSDisk for the disk name.

创建适用于新 VM 的新资源组。Create a new resource group for the new VM.

$destinationResourceGroup = 'myDestinationResourceGroup'
New-AzResourceGroup -Location $location `
   -Name $destinationResourceGroup

设置 OS 磁盘名称。Set the OS disk name.

$osDiskName = 'myOsDisk'

创建托管磁盘。Create the managed disk.

$osDisk = New-AzDisk -DiskName $osDiskName -Disk `
    (New-AzDiskConfig  -Location $location -CreateOption Copy `
    -SourceResourceId $snapshot.Id) `
    -ResourceGroupName $destinationResourceGroup

创建新 VMCreate the new VM

创建新 VM 使用的网络和其他 VM 资源。Create networking and other VM resources to be used by the new VM.

创建子网和虚拟网络Create the subnet and virtual network

为 VM 创建虚拟网络和子网。Create the virtual network and subnet for the VM.

  1. 创建子网。Create the subnet. 本示例在资源组“myDestinationResourceGroup”中创建名为“mySubNet”的子网,并将子网地址前缀设置为 10.0.0.0/24 。This example creates a subnet named mySubNet, in the resource group myDestinationResourceGroup, and sets the subnet address prefix to 10.0.0.0/24.

    $subnetName = 'mySubNet'
    $singleSubnet = New-AzVirtualNetworkSubnetConfig `
       -Name $subnetName `
       -AddressPrefix 10.0.0.0/24
    
  2. 创建虚拟网络。Create the virtual network. 本示例将虚拟网络名称设置为 myVnetName,将位置设置为“中国北部”,将虚拟网络的地址前缀设置为 10.0.0.0/16This example sets the virtual network name to myVnetName, the location to China North, and the address prefix for the virtual network to 10.0.0.0/16.

    $vnetName = "myVnetName"
    $vnet = New-AzVirtualNetwork `
       -Name $vnetName -ResourceGroupName $destinationResourceGroup `
       -Location $location `
       -AddressPrefix 10.0.0.0/16 `
       -Subnet $singleSubnet
    

创建网络安全组和 RDP 规则Create the network security group and an RDP rule

若要使用远程桌面协议 (RDP) 登录到 VM,需要创建一个允许在端口 3389 上进行 RDP 访问的安全规则。To be able to sign in to your VM with remote desktop protocol (RDP), you'll need to have a security rule that allows RDP access on port 3389. 在本示例中,由于新 VM 的 VHD 是从现有专用 VM 创建的,因此,可将源虚拟机中的帐户用于 RDP。In our example, the VHD for the new VM was created from an existing specialized VM, so you can use an account that existed on the source virtual machine for RDP.

本示例将网络安全组 (NSG) 名称设置为 myNsg,将 RDP 规则名称设置为 myRdpRuleThis example sets the network security group (NSG) name to myNsg and the RDP rule name to myRdpRule.

$nsgName = "myNsg"

$rdpRule = New-AzNetworkSecurityRuleConfig -Name myRdpRule -Description "Allow RDP" `
    -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 `
    -SourceAddressPrefix Internet -SourcePortRange * `
    -DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzNetworkSecurityGroup `
   -ResourceGroupName $destinationResourceGroup `
   -Location $location `
   -Name $nsgName -SecurityRules $rdpRule

有关终结点和 NSG 规则的详细信息,请参阅使用 PowerShell 在 Azure 中打开 VM 端口For more information about endpoints and NSG rules, see Opening ports to a VM in Azure by using PowerShell.

创建公共 IP 地址和 NICCreate a public IP address and NIC

若要与虚拟网络中的虚拟机通信,需要一个 公共 IP 地址和网络接口。To enable communication with the virtual machine in the virtual network, you'll need a public IP address and a network interface.

  1. 创建公共 IP。Create the public IP. 在此示例中,公共 IP 地址名称设置为 myIPIn this example, the public IP address name is set to myIP.

    $ipName = "myIP"
    $pip = New-AzPublicIpAddress `
       -Name $ipName -ResourceGroupName $destinationResourceGroup `
       -Location $location `
       -AllocationMethod Dynamic
    
  2. 创建 NIC。Create the NIC. 在此示例中,NIC 名称设置为 myNicNameIn this example, the NIC name is set to myNicName.

    $nicName = "myNicName"
    $nic = New-AzNetworkInterface -Name $nicName `
       -ResourceGroupName $destinationResourceGroup `
       -Location $location -SubnetId $vnet.Subnets[0].Id `
       -PublicIpAddressId $pip.Id `
       -NetworkSecurityGroupId $nsg.Id
    

设置 VM 名称和大小Set the VM name and size

此示例将 VM 名称设置为“myVM”,将 VM 大小设置为“Standard_A2” 。This example sets the VM name to myVM and the VM size to Standard_A2.

$vmName = "myVM"
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_A2"

添加 NICAdd the NIC

$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

添加 OS 磁盘Add the OS disk

使用 Set-AzVMOSDisk 向配置中添加 OS 磁盘。Add the OS disk to the configuration by using Set-AzVMOSDisk. 此示例将磁盘大小设置为 128 GB 并附加托管磁盘作为 Windows OS 磁盘。This example sets the size of the disk to 128 GB and attaches the managed disk as a Windows OS disk.

$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType Standard_LRS `
    -DiskSizeInGB 128 -CreateOption Attach -Windows

完成该 VMComplete the VM

使用 New-AzVM 以及刚才创建的配置创建 VM。Create the VM by using New-AzVM with the configurations that we just created.

New-AzVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm

如果此命令成功,则会显示类似于下面的输出:If this command is successful, you'll see output like this:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK   

验证是否已创建 VMVerify that the VM was created

应会在 Azure 门户的“浏览” > “虚拟机”下看到新建的 VM,也可以使用以下 PowerShell 命令查看该 VM。 You should see the newly created VM either in the Azure portal under Browse > Virtual machines, or by using the following PowerShell commands.

$vmList = Get-AzVM -ResourceGroupName $destinationResourceGroup
$vmList.Name

后续步骤Next steps

登录新虚拟机。Sign in to your new virtual machine. 有关详细信息,请参阅 How to connect and log on to an Azure virtual machine running Windows(如何连接并登录到运行 Windows 的 Azure 虚拟机)。For more information, see How to connect and log on to an Azure virtual machine running Windows.