Get API versions for specific Azure resource

Hello scripters! Today I want to share with you my script for check API versions for specific Azure resource.

For people who are using ARM templates it’s very important to know all of the available API versions for specific Azure resource. If wrong API version will be provided it can cause problem during ARM templates deployment, which is very annoying.

How it works?

Script which I prepared connects to specific Azure subscription and checks for resource which user provided.
If the resource exist script checks namespace of this resource and type.
Once it is done, Get-AzureRmResourceProvider command is used to list all available API versions.

Script:
Login-AzureRmAccount

$Subscriptions = Get-AzureRmSubscription 
$Subscriptions | Format-Table -Property Name, SubscriptionId, State

$SubscriptionID = Read-Host "Provide ID of the subscription"
Try
{
    $Sub = Get-AzureRmSubscription -SubscriptionId $SubscriptionID -ErrorAction Stop| Select-AzureRmSubscription
        
}
Catch
{
    Write-Host "Subscription with ID $SubscriptionID can no be found, script will be stopped!" -ForegroundColor Red
    Break;
}

$ResourceGroupName = Read-Host "Provide name of the resource group"
$ResourceName = Read-Host "Provide name of the resource"

Try
{
    $RespurceType = (Get-AzureRmResource -ResourceName $ResourceName -ResourceGroupName $ResourceGroupName).ResourceType
}
Catch
{
    Write-Error "Resource $ResourceName can not be found under resource group $ResourceGroupName"
    Break;
}

$ProviderNamespace = $RespurceType.Split("/")[0]
$ResourceTypeName = $RespurceType.Split("/")[1]

$APIVersions = ((Get-AzureRmResourceProvider -ProviderNamespace $ProviderNamespace).ResourceTypes | Where-Object ResourceTypeName -eq $ResourceTypeName).ApiVersions

Write-Output "Resource name: " $ResourceName
Write-Output "-----------------------------" 
Write-Output "Provider namespace: " $ProviderNamespace
Write-Output "-----------------------------" 
Write-Output "Resource type: " $ResourceTypeName
Write-Output "-----------------------------" 
Write-Output "Available API versions: " $APIVersions
Result:

If you have a problem with running script, ensure that you have Azure Module installed.

I hope it will be usefull for some of you 😉
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.