Azure Compute Gallery Image Replication and Target Region Check

This script will get your Azure Compute Gallery or Galleries then check all images, latest version, target regions, regional replication counts, storage account types and provisioning status/replication status. It will then create one table with all the information and then a second table with the images that missing in your required regions. This can be used to make sure your AVD images are replicated to the required regions.

          inlineScript: |
              # 2 Tables
              $output = @()
              $missingRegionsOutput = @()
              #Modify required regions
              $requiredRegions = @('East US', 'Central US', 'Qatar Central', 'Southeast Asia', 
              'West Europe')

              # Get all galleries in the subscription
              $allGalleries = Get-AzGallery

              foreach ($gallery in $allGalleries) {
                  $galleryName = $gallery.Name
                  $resourceGroupName = $gallery.ResourceGroupName

                  $imageDefinitions = Get-AzGalleryImageDefinition -ResourceGroupName 
                  $resourceGroupName -GalleryName $galleryName

                  foreach ($imageDef in $imageDefinitions) {
                      $imageVersions = Get-AzGalleryImageVersion -ResourceGroupName 
                      $resourceGroupName -GalleryName $galleryName -GalleryImageDefinitionName 
                      $imageDef.Name
                      $latestVersion = $imageVersions | Sort-Object -Property Name -Descending | 
                      Select-Object -First 1

                      $replicationDetails = $latestVersion.PublishingProfile.TargetRegions

                      # Check if the image is available in all required regions
                      $imageRegions = $replicationDetails | ForEach-Object { $_.Name }
                      $missingRegions = $requiredRegions | Where-Object { $_ -notin $imageRegions 
                      }

                      if ($missingRegions) {
                          $objMissing = [PSCustomObject]@{
                              'ResourceGroupName' = $resourceGroupName
                              'GalleryName'       = $galleryName
                              'ImageName'         = $imageDef.Name
                              'MissingRegions'    = ($missingRegions -join ', ')
                          }
                          $missingRegionsOutput += $objMissing
                      }

                      foreach ($detail in $replicationDetails) {
                          $obj = [PSCustomObject]@{
                              'ResourceGroupName'    = $resourceGroupName
                              'GalleryName'          = $galleryName
                              'ImageName'            = $imageDef.Name
                              'LatestVersion'        = $latestVersion.Name
                              'TargetRegion'         = $detail.Name
                              'RegionalReplicaCount' = $detail.RegionalReplicaCount
                              'StorageAccountType'   = $detail.StorageAccountType
                              'ProvisioningState'    = $latestVersion.ProvisioningState
                          }
                          $output += $obj
                      }
                  }
              }

              # Display details
              $output | Format-Table -AutoSize
              Write-Host "`nImages Missing in Required Regions:" -ForegroundColor Yellow
              $missingRegionsOutput | Format-Table -AutoSize
Share or Save this:
Share