使用 PowerShell 从共享映像库中的托管磁盘或快照创建映像Create an image from a Managed Disk or snapshot in a Shared Image Gallery using PowerShell

如果想要将现有快照或托管磁盘迁移到共享映像库,可直接从该托管磁盘或快照创建共享映像库映像。If you have an existing snapshot or Managed Disk that you would like to migrate into a Shared Image Gallery, you can create a Shared Image Gallery image directly from the Managed Disk or snapshot. 测试新映像后,可删除源托管磁盘或快照。Once you have tested your new image, you can delete the source Managed Disk or snapshot. 还可使用 Azure CLI 从共享映像库中的托管磁盘或快照创建映像。You can also create an image from a Managed Disk or snapshot in a Shared Image Gallery using the Azure CLI.

映像库中的映像具有两个组件,我们将在此示例中创建这两个组件:Images in an image gallery have two components, which we will create in this example:

  • “映像定义”包含有关映像及其使用要求的信息。An Image definition carries information about the image and requirements for using it. 这包括该映像是 Windows 映像还是 Linux 映像、是专用映像还是通用映像,此外还包括发行说明以及最低和最高内存要求。This includes whether the image is Windows or Linux, specialized or generalized, release notes, and minimum and maximum memory requirements. 它是某种映像类型的定义。It is a definition of a type of image.
  • 使用共享映像库时,将使用映像版本来创建 VM。An image version is what is used to create a VM when using a Shared Image Gallery. 可根据环境的需要创建多个映像版本。You can have multiple versions of an image as needed for your environment. 创建 VM 时,将使用该映像版本为 VM 创建新磁盘。When you create a VM, the image version is used to create new disks for the VM. 可以多次使用映像版本。Image versions can be used multiple times.

准备阶段Before you begin

若要完成本文中的操作,必须要有快照或托管磁盘。To complete this article, you must have an snapshot or Managed Disk.

如果要包含数据磁盘,则数据磁盘大小不能超过 1 TB。If you want to include a data disk, the data disk size cannot be more than 1 TB.

通过本文进行操作时,请根据需要替换资源名称。When working through this article, replace the resource names where needed.

获取快照或托管磁盘Get the snapshot or Managed Disk

可以使用 Get-AzSnapshot 查看资源组中可用的快照列表。You can see a list of snapshots that are available in a resource group using Get-AzSnapshot.

get-azsnapshot | Format-Table -Property Name,ResourceGroupName

了解快照的名称及其所在资源组后,可以再次使用 Get-AzSnapshot 来获取快照对象并将其存储在变量中,供稍后使用。Once you know the snapshot name and what resource group it is in, you can use Get-AzSnapshot again to get the snapshot object and store it in a variable to use later. 此示例从“myResourceGroup”资源组获取名为 mySnapshot 的快照,并将其分配给变量 $source 。This example gets an snapshot named mySnapshot from the "myResourceGroup" resource group and assigns it to the variable $source .

$source = Get-AzSnapshot `
   -SnapshotName mySnapshot `
   -ResourceGroupName myResourceGroup

还可使用托管磁盘代替快照。You can also use a Managed Disk instead of a snapshot. 若要获取托管磁盘,请使用 Get-AzDiskTo get a Managed Disk, use Get-AzDisk.

Get-AzDisk | Format-Table -Property Name,ResourceGroupName

然后,获取托管磁盘并将其分配给 $source 变量。Then get the Managed Disk and assign it to the $source variable.

$source = Get-AzDisk `
   -Name myDisk
   -ResourceGroupName myResourceGroup

可以使用相同的 cmdlet 获取要包含在映像中的任何数据磁盘。You can use the same cmdlets to get any data disks that you want to include in your image. 将其分配给变量,稍后在创建映像版本时使用这些变量。Assign them to variables, then use those variables later when you create the image version.

可以按名称列出所有库和映像定义。You can list all of the galleries and image definitions by name. 结果采用 gallery\image definition\image version 格式。The results are in the format gallery\image definition\image version.

Get-AzResource -ResourceType Microsoft.Compute/galleries | Format-Table

找到正确的库后,创建一个变量供稍后使用。Once you find the right gallery, create a variable to use later. 此示例获取 myResourceGroup 资源组中名为 myGallery 的库。This example gets the gallery named myGallery in the myResourceGroup resource group.

$gallery = Get-AzGallery `
   -Name myGallery `
   -ResourceGroupName myResourceGroup

创建映像定义Create an image definition

映像定义为映像创建一个逻辑分组。Image definitions create a logical grouping for images. 它们用于管理有关映像的信息。They are used to manage information about the image. 映像定义名称可能包含大写或小写字母、数字、点、短划线和句点。Image definition names can be made up of uppercase or lowercase letters, digits, dots, dashes and periods.

制作映像定义时,请确保它具有所有正确信息。When making your image definition, make sure is has all of the correct information. 在此示例中,我们假设快照或托管磁盘来自正在使用且尚未通用化的 VM。In this example, we are assuming that the snapshot or Managed Disk are from a VM that is in use, and hasn't been generalized. 如果托管磁盘或快照是从通用化的操作系统获取的(在 Windows 上运行 Sysprep 或在 Linux 上运行 waagent-deprovision-deprovision+user 后),则将 -OsState 更改为 generalizedIf the Managed Disk or snapshot was taken of a generalized OS (after running Sysprep for Windows or waagent -deprovision or -deprovision+user for Linux) then change the -OsState to generalized.

有关可为映像定义指定的值的详细信息,请参阅映像定义For more information about the values you can specify for an image definition, see Image definitions.

使用 New-AzGalleryImageDefinition 创建映像定义。Create the image definition using New-AzGalleryImageDefinition. 在此示例中,映像定义名为 myImageDefinition,适用于通用化的 Windows OS。In this example, the image definition is named myImageDefinition , and is for a specialized Windows OS. 若要使用 Linux OS 创建映像的定义,请使用 -OsType LinuxTo create a definition for images using a Linux OS, use -OsType Linux.

$imageDefinition = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'myImageDefinition' `
   -OsState specialized `
   -OsType Windows `
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU'

购买计划信息Purchase plan information

在某些情况下,当从基于 Azure 市场映像的映像创建 VM 时,需要传入购买计划信息。In some cases, you need to pass purchase plan information in when creating a VM from an image that was based on an Azure Marketplace image. 在这些情况下,我们建议你在映像定义中包含购买计划信息。In these cases, we recommend you include the purchase plan information in the image definition. 在这种情况下,请参阅在创建映像时提供 Azure 市场购买计划信息In this case, see Supply Azure Marketplace purchase plan information when creating images.

创建映像版本Create an image version

使用 New-AzGalleryImageVersion 从快照创建映像版本。Create an image version from the snapshot using New-AzGalleryImageVersion.

允许用于映像版本的字符为数字和句点。Allowed characters for image version are numbers and periods. 数字必须在 32 位整数范围内。Numbers must be within the range of a 32-bit integer. 格式:MajorVersion.MinorVersion.Patch 。Format: MajorVersion . MinorVersion . Patch .

如果希望映像除了包含操作系统磁盘之外还包含数据磁盘,请添加 -DataDiskImage 参数,并将其设置为数据磁盘快照或托管磁盘的 ID。If you want your image to contain a data disk, in addition to the OS disk, then add the -DataDiskImage parameter and set it to the ID of data disk snapshot or Managed Disk.

在此示例中,映像版本为 1.0.0,该版本被复制到中国北部和中国东部数据中心 。In this example, the image version is 1.0.0 and it's replicated to both China North and China East datacenters. 选择复制的目标区域时,请记住,你还需包括源区域作为复制的目标。When choosing target regions for replication, remember that you also have to include the source region as a target for replication.

$region1 = @{Name='China East';ReplicaCount=1}
$region2 = @{Name='China North';ReplicaCount=2}
$targetRegions = @($region1,$region2)
$job = $imageVersion = New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $imageDefinition.Name `
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -TargetRegion $targetRegions  `
   -OSDiskImage @{Source = @{Id=$source.Id}; HostCaching = "ReadOnly" } `
   -PublishingProfileEndOfLifeDate '2025-01-01' `
   -asJob 

可能需要一段时间才能将该映像复制到所有目标区域,因此我们创建了作业,以便可以跟踪进度。It can take a while to replicate the image to all of the target regions, so we have created a job so we can track the progress. 要查看作业的进度,请键入 $job.StateTo see the progress of the job, type $job.State.

$job.State

备注

需等待映像版本完全生成并复制完毕,然后才能使用同一快照来创建另一映像版本。You need to wait for the image version to completely finish being built and replicated before you can use the same snapshot to create another image version.

删除源Delete the source

验证新的映像版本是否正常工作后,可以使用 Remove-AzSnapshotRemove-AzDisk 删除映像的源。Once you have verified that you new image version is working correctly, you can delete the source for the image with either Remove-AzSnapshot or Remove-AzDisk.

后续步骤Next steps

确认复制完成后,可以从专用映像创建 VM。Once you have verified that replication is complete, you can create a VM from the specialized image.

有关如何提供购买计划信息的信息,请参阅在创建映像时提供 Azure 市场购买计划信息For information about how to supply purchase plan information, see Supply Azure Marketplace purchase plan information when creating images.