您现在访问的是微软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

本教程需要 Azure PowerShell 模块 3.6 或更高版本。This tutorial requires the Azure PowerShell module version 3.6 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.

开始之前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.

准备 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. 如果使用自定义映像,则仅需提供自定义映像资源的 ID。With a custom image, you just need to provide the ID of the custom image resource.

在下列脚本中,我们创建了一个 $image 变量,用于存储使用 Get-AzureRmImage 的自定义映像的信息,并使用 Set-AzureRmVMSourceImage,并通过刚创建的 $image 变量指定 ID。In the following script, we create a variable $image to store information about the custom image using Get-AzureRmImage and then we use Set-AzureRmVMSourceImage and specify the ID using the $image variable we just created.

脚本从自定义映像中创建名为“myVMfromImage”的 VM,该自定义映像位于“美国西部”位置名为“myResourceGroupFromImage”的新资源组内。The script creates a VM named myVMfromImage from our custom image in a new resource group named myResourceGroupFromImage in the West US location.

$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

New-AzureRmResourceGroup -Name myResourceGroupFromImage -Location EastUS

$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
    -Name mySubnet `
    -AddressPrefix 192.168.1.0/24

$vnet = New-AzureRmVirtualNetwork `
    -ResourceGroupName myResourceGroupFromImage `
    -Location EastUS `
    -Name MYvNET `
    -AddressPrefix 192.168.0.0/16 `
    -Subnet $subnetConfig

$pip = New-AzureRmPublicIpAddress `
    -ResourceGroupName myResourceGroupFromImage `
    -Location EastUS `
    -Name "mypublicdns$(Get-Random)" `
    -AllocationMethod Static `
    -IdleTimeoutInMinutes 4

  $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
    -Name myNetworkSecurityGroupRuleRDP `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1000 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389 `
    -Access Allow

  $nsg = New-AzureRmNetworkSecurityGroup `
    -ResourceGroupName myResourceGroupFromImage `
    -Location EastUS `
    -Name myNetworkSecurityGroup `
    -SecurityRules $nsgRuleRDP

$nic = New-AzureRmNetworkInterface `
    -Name myNic `
    -ResourceGroupName myResourceGroupFromImage `
    -Location EastUS `
    -SubnetId $vnet.Subnets[0].Id `
    -PublicIpAddressId $pip.Id `
    -NetworkSecurityGroupId $nsg.Id

$vmConfig = New-AzureRmVMConfig `
    -VMName myVMfromImage `
    -VMSize Standard_D1 | Set-AzureRmVMOperatingSystem -Windows `
        -ComputerName myComputer `
        -Credential $cred 

# Here is where we create a variable to store information about the image 
$image = Get-AzureRmImage `
    -ImageName myImage `
    -ResourceGroupName myResourceGroup

# Here is where we specify that we want to create the VM from and image and provide the image ID
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -Id $image.Id

$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id

New-AzureRmVM `
    -ResourceGroupName myResourceGroupFromImage `
    -Location EastUS `
    -VM $vmConfig

映像管理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.