使用 PowerShell 列出、更新和删除映像资源List, update, and delete image resources using PowerShell

可以使用 Azure PowerShell 管理共享映像库资源。You can manage your shared image gallery resources using Azure PowerShell.

列出信息List information

按名称列出所有库。List all galleries by name.

$galleries = Get-AzResource -ResourceType Microsoft.Compute/galleries
$galleries.Name

按名称列出所有映像定义。List all image definitions by name.

$imageDefinitions = Get-AzResource -ResourceType Microsoft.Compute/galleries/images
$imageDefinitions.Name

按名称列出所有映像版本。List all image versions by name.

$imageVersions = Get-AzResource -ResourceType Microsoft.Compute/galleries/images/versions
$imageVersions.Name

删除映像版本。Delete an image version. 此示例将删除名为“1.0.0”的映像版本。This example deletes the image version named 1.0.0.

Remove-AzGalleryImageVersion `
   -GalleryImageDefinitionName myImageDefinition `
   -GalleryName myGallery `
   -Name 1.0.0 `
   -ResourceGroupName myGalleryRG

更新资源Update resources

对于能够更新的内容,存在一些限制。There are some limitations on what can be updated. 以下项目可以更新:The following items can be updated:

共享映像库:Shared image gallery:

  • 说明Description

映像定义:Image definition:

  • 建议的 vCPU 数Recommended vCPUs
  • 建议的内存Recommended memory
  • 说明Description
  • 生命周期终结日期End of life date

映像版本:Image version:

  • 区域副本计数Regional replica count
  • 目标区域数Target regions
  • 从最新项中排除Exclusion from latest
  • 生命周期终结日期End of life date

如果打算添加副本区域,请勿删除源托管映像。If you plan on adding replica regions, do not delete the source managed image. 源托管映像是将映像版本复制到其他区域所需的。The source managed image is needed for replicating the image version to additional regions.

若要更新库的说明,请使用 Update-AzGalleryTo update the description of a gallery, use Update-AzGallery.

Update-AzGallery `
   -Name $gallery.Name ` 
   -ResourceGroupName $resourceGroup.Name

此示例演示如何使用 Update-AzGalleryImageDefinition 来更新映像定义的生命周期结束日期。This example shows how to use Update-AzGalleryImageDefinition to update the end-of-life date for our image definition.

Update-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -Name $galleryImage.Name `
   -ResourceGroupName $resourceGroup.Name `
   -EndOfLifeDate 01/01/2030

此示例演示如何使用 Update-AzGalleryImageVersion 来排除此映像版本,使之不能用作最新映像。 This example shows how to use Update-AzGalleryImageVersion to exclude this image version from being used as the latest image.

Update-AzGalleryImageVersion `
   -GalleryImageDefinitionName $galleryImage.Name `
   -GalleryName $gallery.Name `
   -Name $galleryVersion.Name `
   -ResourceGroupName $resourceGroup.Name `
   -PublishingProfileExcludeFromLatest

清理资源Clean up resources

删除资源时,需从嵌套资源中的最后一项(映像版本)开始。When deleting resources, you need to start with last item in the nested resources - the image version. 删除版本以后,即可删除映像定义。Once versions are deleted, you can delete the image definition. 必须先删除库下的所有资源,然后才能删除库。You can't delete the gallery until all resources beneath it have been deleted.

$resourceGroup = "myResourceGroup"
$gallery = "myGallery"
$imageDefinition = "myImageDefinition"
$imageVersion = "myImageVersion"

Remove-AzGalleryImageVersion `
   -GalleryImageDefinitionName $imageDefinition `
   -GalleryName $gallery `
   -Name $imageVersion `
   -ResourceGroupName $resourceGroup

Remove-AzGalleryImageDefinition `
   -ResourceGroupName $resourceGroup `
   -GalleryName $gallery `
   -GalleryImageDefinitionName $imageDefinition

Remove-AzGallery `
   -Name $gallery `
   -ResourceGroupName $resourceGroup

Remove-AzResourceGroup -Name $resourceGroup