使用 PowerShell 复制另一个库中的映像Copy an image from another gallery using PowerShell

如果你的组织有多个库,可以从其他库中存储的映像创建映像。If you have multiple galleries in your organization, you can create images from images stored in other galleries. 例如,你可能会使用一个开发和测试库用于创建和测试新映像。For example, you might have a development and test gallery for creating and testing new images. 准备好在生产环境中使用这些映像时,可以遵循本示例将它们复制到生产库中。When they are ready to be used in production, you can copy them into a production gallery using this example. 也可以使用 Azure CLI 从另一个库中的映像创建映像。You can also create an image from an image in another gallery using the Azure CLI.

准备阶段Before you begin

若要完成本文,必须有一个现有的源库、映像定义和映像版本。To complete this article, you must have an existing source gallery, image definition, and image version. 此外,还应有一个目标库。You should also have a destination gallery.

必须将源映像版本复制到目标库所在的区域。The source image version must be replicated to the region where your destination gallery is located.

我们将在目标库中创建新的映像定义和映像版本。We will be creating a new image definition and image version in your destination gallery.

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

获取源映像Get the source image

需要源映像定义中的信息,以便可以在目标库中创建该映像的副本。You will need information from the source image definition so you can create a copy of it in your destination gallery.

使用 Get-AzResource cmdlet 列出有关现有库、映像定义和映像版本的信息。List information about the existing galleries, image definitions, and image versions using the Get-AzResource cmdlet.

结果采用 gallery\image definition\image version 格式。The results are in the format gallery\image definition\image version.

Get-AzResource `
   -ResourceType Microsoft.Compute/galleries/images/versions | `
   Format-Table -Property Name,ResourceGroupName

获取全部所需信息后,可以使用 Get-AzGalleryImageVersion 获取源映像版本的 ID。Once you have all of the information you need, you can get the ID of the source image version using Get-AzGalleryImageVersion. 在本示例中,我们将在 myResourceGroup 资源组的 myGallery 源库中,获取 myImageDefinition 定义的 1.0.0 映像版本。In this example, we are getting the 1.0.0 image version, of the myImageDefinition definition, in the myGallery source gallery, in the myResourceGroup resource group.

$sourceImgVer = Get-AzGalleryImageVersion `
   -GalleryImageDefinitionName myImageDefinition `
   -GalleryName myGallery `
   -ResourceGroupName myResourceGroup `
   -Name 1.0.0

创建映像定义Create the image definition

需要创建一个与源的映像定义相匹配的新映像定义。You need to create a new image definition that matches the image definition of your source. 可以使用 Get-AzGalleryImageDefinition 查看重新创建映像定义所需的全部信息。You can see all of the information you need to recreate the image definition using Get-AzGalleryImageDefinition.

Get-AzGalleryImageDefinition `
   -GalleryName myGallery `
   -ResourceGroupName myResourceGroup `
   -Name myImageDefinition

输出将如下所示:The output will look something like this:

{
  "description": null,
  "disallowed": null,
  "endOfLifeDate": null,
  "eula": null,
  "hyperVgeneration": "V1",
  "id": "/subscriptions/1111abcd-1a23-4b45-67g7-1234567de123/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition",
  "identifier": {
    "offer": "myOffer",
    "publisher": "myPublisher",
    "sku": "mySKU"
  },
  "location": "chinaeast",
  "name": "myImageDefinition",
  "osState": "Specialized",
  "osType": "Windows",
  "privacyStatementUri": null,
  "provisioningState": "Succeeded",
  "purchasePlan": null,
  "recommended": null,
  "releaseNoteUri": null,
  "resourceGroup": "myGalleryRG",
  "tags": null,
  "type": "Microsoft.Compute/galleries/images"
}

使用 New-AzGalleryImageDefinition cmdlet 以及上述输出中的信息,在目标库中创建新的映像定义。Create a new image definition, in your destination gallery, using the New-AzGalleryImageDefinition cmdlet and the information from the output above.

在本示例中,映像定义在名为 myDestinationGallery 的库中命名为 myDestinationImgDefIn this example, the image definition is named myDestinationImgDef in the gallery named myDestinationGallery.

$destinationImgDef  = New-AzGalleryImageDefinition `
   -GalleryName myDestinationGallery `
   -ResourceGroupName myDestinationRG `
   -Location ChinaNorth `
   -Name 'myDestinationImgDef' `
   -OsState specialized `
   -OsType Windows `
   -HyperVGeneration v1
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU'

创建映像版本Create the image version

使用 New-AzGalleryImageVersion 创建映像版本。Create an image version using New-AzGalleryImageVersion. 需要在 --managed-image 参数中传递源映像的 ID,用于在目标库中创建映像版本。You will need to pass in the ID of the source image in the --managed-image parameter for creating the image version in your destination gallery.

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

在本示例中,目标库在位于“中国北部”位置的 myDestinationRG 资源组中命名为 myDestinationGalleryIn this example, the destination gallery is named myDestinationGallery, in the myDestinationRG resource group, in the China North location. 映像版本为 1.0.0,我们将在“中国东部”区域创建 1 个副本,并在“中国北部”区域创建 2 个副本。 The version of our image is 1.0.0 and we are going to create 1 replica in the China East region, and 2 replicas in the China North region.

$region1 = @{Name='China East';ReplicaCount=1}
$region2 = @{Name='China North';ReplicaCount=2}
$targetRegions = @($region1,$region2)

$job = $imageVersion = New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $destinationImgDef.Name`
   -GalleryImageVersionName '1.0.0' `
   -GalleryName myDestinationGallery `
   -ResourceGroupName myDestinationRG `
   -Location ChinaNorth `
   -TargetRegion $targetRegions  `
   -Source $sourceImgVer.Id.ToString() `
   -PublishingProfileEndOfLifeDate '2020-12-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 managed image to create another image version.

创建映像版本时,还可以通过添加 -StorageAccountType Premium_LRS 在高级存储中存储映像。You can also store your image in Premium storage by a adding -StorageAccountType Premium_LRS when you create the image version.

后续步骤Next steps

通用化专用化映像版本创建 VM。Create a VM from a generalized or a specialized image version.