Skip to content

Instantly share code, notes, and snippets.

@mikefrobbins
Last active October 2, 2023 12:44
Show Gist options
  • Save mikefrobbins/73757ca0c9c098d02e0758921a0d48d0 to your computer and use it in GitHub Desktop.
Save mikefrobbins/73757ca0c9c098d02e0758921a0d48d0 to your computer and use it in GitHub Desktop.
Status of Azure VM Image
<#
Prerequisites
An Azure subscription is required.
Install the Az PowerShell module from the PowerShell Gallery:
Install-Module -Name Az
Login to Azure:
Connect-AzAccount
#>
$subscriptionId = (Get-AzSubscription).Id
Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.Compute/locations/westeurope/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/WindowsServer/skus/2016-Datacenter/versions/14393.3866.2008081933?api-version=2023-07-01" -OutVariable results
($results.Content | ConvertFrom-Json -AsHashtable).values | Select-Object -Property @{label='ImageName';expression={$_.message -replace '^.*Sku: |,.*$'}}, @{label='Status';expression={$_.code}}
@irwins
Copy link

irwins commented Oct 2, 2023

$subscriptionId = '<fill in your subscription id>'
$location = 'westeurope'

$argQuery = @"
resources
|where type == "microsoft.compute/virtualmachines"
|extend imagePublisher = tostring(properties.storageProfile.imageReference.publisher),
        imageOffer = tostring(properties.storageProfile.imageReference.offer),
        imageSku = tostring(properties.storageProfile.imageReference.sku),
        imageVersion = tostring(properties.storageProfile.imageReference.version),
        imageExactVersion = tostring(properties.storageProfile.imageReference.exactVersion)
| distinct imagePublisher,imageOffer,imageSku,imageExactVersion
| where isnotempty(imagePublisher)
"@

$argResults = Search-AzGraph -Query $argQuery -UseTenantScope -First 1000

$imageStatus = $argResults |
ForEach-Object{
    $publisher = $_.ImagePublisher
    $offer = $_.ImageOffer
    $sku = $_.ImageSKU
    $version = $_.ImageExactVersion

    $uri = 'https://management.azure.com/subscriptions/{0}/providers/Microsoft.Compute/locations/{1}/publishers/{2}/artifacttypes/vmimage/offers/{3}/skus/{4}/versions/{5}?api-version=2023-07-01' -f @(
        $subscriptionId
        $location
        $publisher
        $offer
        $sku
        $version
    )

    $result = Invoke-AzRestMethod -Uri $uri -Method GET

    $imageState = 'ImageVersionActive'

    if ($result.StatusCode -eq 404) {
        $imageState = 'ImageVersionDeprecated'
    }

    [PSCustomObject]@{
        ImagePublisher = $publisher
        ImageOffer = $offer
        ImageSKU = $sku
        ImageExactVersion = $version
        ImageState = $imageState
    }
}

"Images count deprecated: $(($imageStatus | Where-Object{$_.ImageState -eq 'ImageVersionDeprecated'}).count)"
"Images count active    : $(($imageStatus | Where-Object{$_.ImageState -eq 'ImageVersionActive'}).count)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment