0
\$\begingroup\$

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?

\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.