Vyas Bharghava
Vyas Bharghava

Reputation: 6510

Flag Outdated Version Numbers in an ARM Template

I found a script that used the older RM Powershell command to get latest versions of the Azure resource provider such as Microsoft.Compute/availabilitySets etc. (aka. Type) and compares them to the resources on the specified ARM template:

Get-Content leanArmTemplate.json |
ConvertFrom-Json |
Select-Object -ExpandProperty resources |
ForEach-Object {
    $latestApiVersion = Get-AzureRmResourceProviderLatestApiVersion -Type $_.type
    [PsCustomObject]@{
        Type           = $_.type
        UsedApiVersion = $_.apiVersion
        LatestVersion  = $latestApiVersion
        Latest         = $_.apiVersion -eq $latestApiVersion
    }
}

However, the latest version Get-AzResourceProvider takes a namespace (Microsoft.Compute). I wrote the following:

Param (
    [string]$FileName = ""
)

$extension = ($FileName -split '\.' | Select-Object -Last 1).ToLower()

if ($FileName -eq "" || $extension -ne "json")
{
    Write-Host "Must specifiy an ARM template"
    return
}

Get-Content $FileName |
ConvertFrom-Json |
Select-Object -ExpandProperty resources |
ForEach-Object {
    $resource = $_
    $splitTypes = $resource.type -split '/', 5
    $length = $splitTypes.Length
    $needed = $length - 1
    $splitTypes[1] = ($splitTypes | Select-Object -Last $needed) -join '/'


    (Get-AzResourceProvider -ProviderNamespace $splitTypes[0]).ResourceTypes | Where-Object { $_.ResourceTypeName -contains $splitTypes[1] } | ForEach-Object {
       [PsCustomObject]@{
            Type           = $resource.type
            UsedApiVersion = $resource.apiVersion
            LatestVersion  = $_.ApiVersions[0]
            Latest         = $resource.apiVersion -eq $_.ApiVersions[0]
        }
    }
}

Seems to work for my current templates. Any suggestions for improvement / obvious bugs / simplification?

Upvotes: 0

Views: 30

Answers (1)

bmoore-msft
bmoore-msft

Reputation: 8717

https://aka.ms/arm-ttk will test apiVersions in templates.

Upvotes: 1

Related Questions