您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 PowerShell 创建 Azure VM 的自定义映像Create a custom image of an Azure VM using PowerShell

自定义映像类似于应用商店映像,不同的是自定义映像的创建者是自己。Custom images are like marketplace images, but you create them yourself. 自定义映像可用于启动配置,例如预加载应用程序、应用程序配置和其他 OS 配置。Custom images can be used to bootstrap configurations such as preloading applications, application configurations, and other OS configurations. 在本教程中,将创建自己的 Azure 虚拟机自定义映像。In this tutorial, you create your own custom image of an Azure virtual machine. 你将学习如何: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 Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/powershellhttps://shell.azure.com/powershell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 AzureRM 模块 5.6.0 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the AzureRM module version 5.6.0 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module.

准备 VMPrepare VM

要创建虚拟机的映像,需通过以下方式准备 VM:通用化 VM、解除分配,然后在 Azure 中将源 VM 标记为通用化。To create an image of a virtual machine, you need to prepare the VM by generalizing the VM, deallocating, and then marking the source VM as generalized in 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.exe。Change 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-AzureRmVM 对 VM 解除分配。Deallocated the VM using Stop-AzureRmVM.

Stop-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM -Force

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

Set-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM -Generalized

创建映像Create the image

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

获取虚拟机。Get the virtual machine.

$vm = Get-AzureRmVM -Name myVM -ResourceGroupName myResourceGroup

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

$image = New-AzureRmImageConfig -Location EastUS -SourceVirtualMachineId $vm.ID 

创建映像。Create the image.

New-AzureRmImage -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 与使用 Marketplace 映像创建 VM 非常相似。Creating a VM from a custom image is very similar to creating a VM using a Marketplace image. 如果使用 Marketplace 映像,需提供有关映像、映像提供程序、产品/服务、SKU 和版本的信息。When you use a Marketplace image, you have to provide the information about the image, image provider, offer, SKU and version. 使用为 New-AzureRMVM cmdlet 设置的简化参数时,如果自定义映像位于同一资源组中,则只需提供该映像的名称。Using the simplified parameter set for the New-AzureRMVM 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, in the myResourceGroup.

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

映像管理Image management

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

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

$images = Find-AzureRMResource -ResourceType Microsoft.Compute/images 
$images.name

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

Remove-AzureRmImage `
    -ImageName myOldImage `
    -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 highly available virtual machines.