In some common scenarios, it needed to list all the web apps runtime and stack versions for a subscriptions. While the stack version for Windows web apps and Linux Web apps need to be retrieved from different places.
In this blog, it shows how to retrieve the runtime and stack version both for Windows web apps and Linux web app in batch with Powershell script.
- For Linux web apps, the runtime and stack version can be retrieved from az webapp show linuxFxVersion property.
- For Windows web apps, the runtime and stack version can be retrieved by List Metadata REST API.
With the above information, we can use the script below to list all the web apps runtime and stack version under a subscription:
$runtimes = [System.Collections.Generic.List[object]]@()
Write-Progress "Fetching subscription id"
$sub = "ed914990-3a09-4e86-8331-20121d82be0f" #your subscription
az account set --subscription $sub
# Get all resource groups in the subscription
Write-Progress "Searching for resource groups"
$groups = (az group list --query "[].name" -o tsv)
# Set counter for group progress
$groupCounter = 1;
# Loop through each resource group to find all the web apps in it
foreach($group in $groups) {
# Find web apps in the specified group
Write-Progress "Searching for web apps in resource group $group" -PercentComplete (($groupCounter / $groups.Count) * 100)
$apps =(az webapp list -g $group --query "[?kind=='app' || kind=='app,linux'].name" -o tsv)
# Iterate the web apps
foreach($app in $apps) {
# Query the web app for versions
Write-Progress "Querying web app $app"
$appConfig = (az webapp show -n $app -g $group --query "{java:siteConfig.javaversion,netFramework:siteConfig.netFrameworkVersion,php:siteConfig.phpVersion,python:siteConfig.pythonVersion,NODE:siteConfig.nodeVersion,linux:siteConfig.linuxFxVersion}") | ConvertFrom-Json
# Define an output object
$output = [PSCustomObject]@{
group = $group
name = $app
host = $null
runtime = $null
version = $null
}
# Determine which type of app service it is and get the values accordingly
if($appConfig.linux -eq "") {
# Windows OS
$output.host = "windows"
# Query the app config to get the metadata for the current stack
$uri = "https://management.azure.com/subscriptions/$sub/resourceGroups/$group/providers/Microsoft.Web/sites/$app/config/metadata/list?api-version=2024-04-01"
$output.runtime = (az rest --method post --uri $uri --query "properties.CURRENT_STACK" -o tsv)
# Determine the version of the relevant stack
$output.version = switch($output.runtime) {
"dotnet" {$appConfig.netFramework}
"dotnetcore" {$null}
"python" {$appConfig.python}
"php" {$appConfig.php}
"java" {$appConfig.java}
"node" {$appConfig.node}
default {$null}
}
} else {
# Linux OS
$output.host = "linux"
# Split out the stack from the version
$output.runtime = $appConfig.linux.split("|")[0]
$output.version = $appConfig.linux.split("|")[1]
}
$runtimes.Add($output)
}
$groupCounter = $groupCounter + 1
}
Write-Output $runtimes
The output of above script like below which contains Resource Group, web app name, OS, runtime and stack version:
If you want to filter with more details, such as filtering the apps which are hosting with node 18, then you can add additional script as below:
#filter app with node 18 version
$NODEapps = $runtimes | Where-Object { ($_.runtime -eq 'NODE') -and ($_.version -like '*18*') }
Write-OutputWrite-Output "Web apps with node 18 as below"
$NODEapps
The output will only list the web apps which stack version is node 18.