上传通用化 VHD 并使用它在 Azure 中创建新 VMUpload a generalized VHD and use it to create new VMs in Azure

本文逐步讲解如何使用 PowerShell 将通用化 VM 的 VHD 上传到 Azure、从该 VHD 创建映像,然后从该映像创建新 VM。This article walks you through using PowerShell to upload a VHD of a generalized VM to Azure, create an image from the VHD, and create a new VM from that image. 可以上传从本地虚拟化工具或其他云导出的 VHD。You can upload a VHD exported from an on-premises virtualization tool or from another cloud. 对新的 VM 使用托管磁盘可以简化 VM 管理,在将 VM 置于可用性集中时提供更好的可用性。Using Managed Disks for the new VM simplifies the VM management and provides better availability when the VM is placed in an availability set.

有关示例脚本,请参阅将 VHD 上传到 Azure 并创建新的 VM 的示例脚本For a sample script, see Sample script to upload a VHD to Azure and create a new VM.

准备阶段Before you begin

使用 Sysprep 通用化源 VMGeneralize the source VM by using Sysprep

需要先对 VM 运行 Sysprep(如果尚未这样做),然后再将 VHD 上传到 Azure。If you haven't already, you need to Sysprep the VM before uploading the VHD to Azure. Sysprep 将删除所有个人帐户信息及其他某些数据,并准备好要用作映像的计算机。Sysprep removes all your personal account information, among other things, and prepares the machine to be used as an image. 有关 Sysprep 的详细信息,请参阅 Sysprep 概述For details about Sysprep, see the Sysprep Overview.

确保 Sysprep 支持计算机上运行的服务器角色。Make sure the server roles running on the machine are supported by Sysprep. 有关详细信息,请参阅 Sysprep Support for Server Roles(Sysprep 对服务器角色的支持)。For more information, see Sysprep Support for Server Roles.

重要

如果计划在首次将 VHD 上传到 Azure 之前运行 Sysprep,请确保先准备好 VMIf you plan to run Sysprep before uploading your VHD to Azure for the first time, make sure you have prepared your VM.

  1. 登录到 Windows 虚拟机。Sign in to the Windows virtual machine.

  2. 以管理员身份打开“命令提示符”窗口。Open the Command Prompt window as an administrator. 将目录切换到 %windir%\system32\sysprep,然后运行 sysprep.exeChange the directory to %windir%\system32\sysprep, and then run sysprep.exe.

  3. 在“系统准备工具”对话框中,选择“进入系统全新体验(OOBE)”,确保已选中“通用化”复选框。 In the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE) , and make sure that the Generalize check box is enabled.

  4. 在“关机选项”中选择“关机”。For Shutdown Options , select Shutdown .

  5. 选择“确定” 。Select OK .

    启动 Sysprep

  6. 在 Sysprep 完成时,它会关闭虚拟机。When Sysprep finishes, it shuts down the virtual machine. 不要重新启动 VM。Do not restart the VM.

上传 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.

将 VHD 上传到托管磁盘后,需要使用 Get-AzDisk 获取托管磁盘。Once the VHD is uploaded to the managed disk, you need to use Get-AzDisk to get the managed disk.

# Sign in the Azure China Cloud
Connect-AzAccount -Environment AzureChinaCloud

$disk = Get-AzDisk -ResourceGroupName 'myResourceGroup' -DiskName 'myDiskName'

创建映像Create the image

从通用化 OS 托管磁盘创建托管映像。Create a managed image from your generalized OS managed disk. 将以下值替换为自己的信息。Replace the following values with your own information.

首先,设置一些变量:First, set some variables:

$location = 'China East'
$imageName = 'myImage'
$rgName = 'myResourceGroup'

使用托管磁盘创建映像。Create the image using your managed disk.

$imageConfig = New-AzImageConfig `
   -Location $location
$imageConfig = Set-AzImageOsDisk `
   -Image $imageConfig `
   -OsState Generalized `
   -OsType Windows `
   -ManagedDiskId $disk.Id

创建映像。Create the image.

$image = New-AzImage `
   -ImageName $imageName `
   -ResourceGroupName $rgName `
   -Image $imageConfig

创建 VMCreate the VM

现在,你已有了一个映像,可以从该映像创建一个或多个新 VM。Now that you have an image, you can create one or more new VMs from the image. 本示例从 myResourceGroup 中的 myImage 创建名为 myVM 的 VM。This example creates a VM named myVM from myImage , in myResourceGroup .

New-AzVm `
    -ResourceGroupName $rgName `
    -Name "myVM" `
    -Image $image.Id `
    -Location $location `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNSG" `
    -PublicIpAddressName "myPIP" `
    -OpenPorts 3389

后续步骤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.