教程:使用 Azure PowerShell 创建 Azure VM 的自定义映像Tutorial: Create a custom image of an Azure VM with Azure PowerShell

自定义映像类似于市场映像,不同的是自定义映像的创建者是自己。Custom images are like marketplace images, but you create them yourself. 自定义映像可用于启动部署并确保多个 VM 的一致性。Custom images can be used to bootstrap deployments and ensure consistency across multiple VMs. 在本教程中,需使用 PowerShell 创建自己的 Azure 虚拟机自定义映像。In this tutorial, you create your own custom image of an Azure virtual machine using PowerShell. 你将学习如何执行以下操作:You learn how to:

  • 使用 Sysprep 通用化 VMSysprep and generalize VMs
  • 创建自定义映像Create a custom image
  • 从自定义映像创建 VMCreate a VM from a custom image
  • 列出订阅中的所有映像List all the images in your subscription
  • 删除映像Delete an image

开始之前Before you begin

下列步骤详细说明了如何将现有 VM 转换为可重用自定义映像,用于创建新的 VM 实例。The steps below detail how to take an existing VM and turn it into a re-usable custom image that you can use to create new VM instances.

若要完成本教程中的示例,必须具备现有虚拟机。To complete the example in this tutorial, you must have an existing virtual machine. 如果需要,此脚本示例可为你创建一个虚拟机。If needed, this script sample can create one for you. 按照教程进行操作时,请根据需要替换资源组和 VM 名称。When working through the tutorial, replace the resource group and VM names where needed.

启动 Azure PowerShellLaunch Azure PowerShell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块This article has been updated to use the new Azure PowerShell Az module. AzureRM 将继续获取关键的 bug 修复更新,但新功能将只出现在 Az 模块中。AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.

  • 若要在本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az module locally, see Install Azure PowerShell.
  • 如果在本地安装 Az 模块,可通过运行 Enable-AzureRmAlias 来启用 AzureRM 兼容性。If you install the Az module locally, you can enable the AzureRM compatibility by running Enable-AzureRmAlias.

准备 VMPrepare VM

若要创建虚拟机的映像,需通过以下方式准备源 VM:通用化该 VM、解除分配,然后在 Azure 中将其标记为通用化。To create an image of a virtual machine, you need to prepare the source VM by generalizing it, deallocating, and then marking it as generalized with Azure.

使用 Sysprep 通用化 Windows VMGeneralize the Windows VM using Sysprep

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 How to Use Sysprep: An Introduction.

  1. 连接到虚拟机。Connect to the 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 selected.
  4. 在“关机选项”中选择“关机”,然后单击“确定”。In Shutdown Options, select Shutdown and then click OK.
  5. Sysprep 在完成运行后会关闭虚拟机。When Sysprep completes, it shuts down the virtual machine. 请勿重启 VM。Do not restart the VM.

解除分配 VM 并将其标记为通用化Deallocate and mark the VM as generalized

若要创建映像,需解除分配 VM,并在 Azure 中将其标记为通用化。To create an image, the VM needs to be deallocated and marked as generalized in Azure.

使用 Stop-AzVM 对 VM 解除分配。Deallocate the VM using Stop-AzVM.

Connect-AzAccount -Environment AzureChinaCloud
Stop-AzVM `
   -ResourceGroupName myResourceGroup `
   -Name myVM -Force

使用 Set-AzVm 将虚拟机的状态设置为 -GeneralizedSet the status of the virtual machine to -Generalized using Set-AzVm.

Set-AzVM `
   -ResourceGroupName myResourceGroup `
   -Name myVM -Generalized

创建映像Create the image

现在,可以使用 New-AzImageConfigNew-AzImage 来创建 VM 的映像。Now you can create an image of the VM by using New-AzImageConfig and New-AzImage. 以下示例从名为 myVM 的 VM 创建名为 myImage 的映像。The following example creates an image named myImage from a VM named myVM.

获取虚拟机。Get the virtual machine.

$vm = Get-AzVM `
   -Name myVM `
   -ResourceGroupName myResourceGroup

创建映像配置。Create the image configuration.

$image = New-AzImageConfig `
   -Location ChinaEast `
   -SourceVirtualMachineId $vm.ID 

创建映像。Create the image.

New-AzImage `
   -Image $image `
   -ImageName myImage `
   -ResourceGroupName myResourceGroup

从映像创建 VMCreate VMs from the image

在已有映像之后,可以从该映像创建一个或多个新 VM。Now that you have an image, you can create one or more new VMs from the image. 从自定义映像创建 VM 与使用市场映像创建 VM 很相似。Creating a VM from a custom image is similar to creating a VM using a Marketplace image. 如果使用市场映像,需提供有关映像、映像提供程序、产品/服务、SKU 和版本的信息。When you use a Marketplace image, you have to provide the information about the image, image provider, offer, SKU, and version. 使用为 New-AzVM cmdlet 设置的简化参数时,如果自定义映像位于同一资源组中,则只需提供该映像的名称。Using the simplified parameter set for the New-AzVM cmdlet, you just need to provide the name of the custom image as long as it is in the same resource group.

本示例从“myResourceGroup”中的“myImage”映像创建名为“myVMfromImage”的 VM。This example creates a VM named myVMfromImage from the myImage image, in myResourceGroup.

New-AzVm `
    -ResourceGroupName "myResourceGroup" `
    -Name "myVMfromImage" `
    -ImageName "myImage" `
    -Location "China East" `
    -VirtualNetworkName "myImageVnet" `
    -SubnetName "myImageSubnet" `
    -SecurityGroupName "myImageNSG" `
    -PublicIpAddressName "myImagePIP" `
    -OpenPorts 3389

映像管理Image management

下面是一些常见的托管映像任务示例,并说明了如何使用 PowerShell 完成这些任务。Here are some examples of common managed image tasks and how to complete them using PowerShell.

按名称列出所有映像。List all images by name.

$images = Get-AzResource -ResourceType Microsoft.Compute/images 
$images.name

删除映像。Delete an image. 此示例将从 myResourceGroup 中删除名为 myImage 的映像。This example deletes the image named myImage from the myResourceGroup.

Remove-AzImage `
    -ImageName myImage `
    -ResourceGroupName myResourceGroup

后续步骤Next steps

在本教程中,你已创建了一个自定义 VM 映像。In this tutorial, you created a custom VM image. 你已了解如何:You learned how to:

  • 使用 Sysprep 通用化 VMSysprep and generalize VMs
  • 创建自定义映像Create a custom image
  • 从自定义映像创建 VMCreate a VM from a custom image
  • 列出订阅中的所有映像List all the images in your subscription
  • 删除映像Delete an image

请转到下一教程,了解如何创建高度可用的虚拟机。Advance to the next tutorial to learn about how to create highly available virtual machines.