使用 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.

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.

选项 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.

获取存储帐户Get the storage account

Azure 中需要有一个存储帐户用于存储上传的 VHD。You'll need a storage account in Azure to store the uploaded VHD. 可以使用现有存储帐户,也可以创建新存储帐户。You can either use an existing storage account or create a new one.

显示可用的存储帐户。Show the available storage accounts.

Get-AzStorageAccount

若要使用现有存储帐户,请转到上传 VHD 部分。To use an existing storage account, proceed to the Upload the VHD section.

创建存储帐户。Create a storage account.

  1. 需要使用要在其中创建存储帐户的资源组的名称。You'll need the name of the resource group where the storage account will be created. 使用 Get-AzResourceGroup 查看订阅中的所有资源组。Use Get-AzResourceGroup see all the resource groups that are in your subscription.

    Get-AzResourceGroup
    

    在“中国北部”区域中创建名为 myResourceGroup 的资源组。 Create a resource group named myResourceGroup in the China North region.

    New-AzResourceGroup `
       -Name myResourceGroup `
       -Location "China North"
    
  2. 使用 New-AzStorageAccount cmdlet 在新资源组中创建名为 mystorageaccount 的存储帐户。Create a storage account named mystorageaccount in the new resource group by using the New-AzStorageAccount cmdlet.

    New-AzStorageAccount `
       -ResourceGroupName myResourceGroup `
       -Name mystorageaccount `
       -Location "China North" `
       -SkuName "Standard_LRS" `
       -Kind "Storage"
    

将 VHD 上传到存储帐户Upload the VHD to your storage account

使用 Add-AzVhd cmdlet 将 VHD 上传到存储帐户中的容器。Use the Add-AzVhd cmdlet to upload the VHD to a container in your storage account. 本示例将文件 myVHD.vhd 从 "C:\Users\Public\Documents\Virtual hard disks" 上传到 myResourceGroup 资源组中名为 mystorageaccount 的存储帐户。This example uploads the file myVHD.vhd from "C:\Users\Public\Documents\Virtual hard disks" to a storage account named mystorageaccount in the myResourceGroup resource group. 该文件存储在名为 mycontainer 的容器中,新文件名为 myUploadedVHD.vhdThe file is stored in the container named mycontainer and the new file name will be myUploadedVHD.vhd.

$resourceGroupName = "myResourceGroup"
$urlOfUploadedVhd = "https://mystorageaccount.blob.core.chinacloudapi.cn/mycontainer/myUploadedVHD.vhd"
Add-AzVhd -ResourceGroupName $resourceGroupName `
   -Destination $urlOfUploadedVhd `
   -LocalFilePath "C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd"

如果该命令成功,则会显示类似于下面的响应:If the commands are successful, you'll get a response that looks similar to this:

MD5 hash is being calculated for the file C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd.
MD5 hash calculation is completed.
Elapsed time for the operation: 00:03:35
Creating new page blob of size 53687091712...
Elapsed time for upload: 01:12:49

LocalFilePath           DestinationUri
-------------           --------------
C:\Users\Public\Doc...  https://mystorageaccount.blob.core.chinacloudapi.cn/mycontainer/myUploadedVHD.vhd

根据网络连接速度和 VHD 文件的大小,可能需要一段时间才能完成此命令。This command may take a while to complete, depending on your network connection and the size of your VHD file.

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

使用 New-AzDisk,基于存储帐户中的专用 VHD 创建托管磁盘。Create a managed disk from the specialized VHD in your storage account by using New-AzDisk. 此示例使用“myOSDisk1”作为 磁盘名称,将磁盘放置在“Standard_LRS” 存储中,并使用 https://storageaccount.blob.core.chinacloudapi.cn/vhdcontainer/osdisk.vhd 作为源 VHD 的 URI。This example uses myOSDisk1 for the disk name, puts the disk in Standard_LRS storage, and uses https://storageaccount.blob.core.chinacloudapi.cn/vhdcontainer/osdisk.vhd as the URI for the source VHD.

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

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

从上传的 VHD 创建新 OS 磁盘。Create the new OS disk from the uploaded VHD.

$sourceUri = 'https://storageaccount.blob.core.chinacloudapi.cn/vhdcontainer/osdisk.vhd'
$osDiskName = 'myOsDisk'
$osDisk = New-AzDisk -DiskName $osDiskName -Disk `
    (New-AzDiskConfig -AccountType Standard_LRS  `
    -Location $location -CreateOption Import `
    -SourceUri $sourceUri) `
    -ResourceGroupName $destinationResourceGroup

选项 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.

拍摄 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.