Powershell Script to Retrieve Computer Names for Your Azure App Services

I love Live Metrics in Azure Application Insights. It's great to have a list of servers and be able to see CPU and memory usage (among other stats) in real-time. Great! But what are those server names? How do I match them up to running instances in my app services?

I could not find the "server name" for an app service anywhere. I tried poking around in Resource Explorer to no avail.

I turns out the ComputerName is an environment variable for the instances in the app service, so it was time to use Powershell to iterate through alll my app services and instances and list the computer names.

Running the script below I am able to produce a list of app services and thier associated computer names (or server names):

$subscriptionId = "<your subscription id>"

Connect-AzAccount

Set-AzContext -SubscriptionId $subscriptionId

$websites = Get-AzResource -ResourceType Microsoft.Web/sites

foreach($website in $websites) {
    $resoureGroupName = $website.ResourceGroupName
    $websiteName = $website.Name
    $instances = Get-AzResource -ResourceGroupName $resoureGroupName `
    -ResourceType Microsoft.Web/sites/instances `
    -ResourceName $websiteName `
    -ApiVersion 2018-02-01

    foreach($instance in $instances) {
        $instanceName = $instance.Name
        
        try {
            $processes = Get-AzResource -ResourceGroupName $resoureGroupName `
                -ResourceType Microsoft.Web/sites/instances/processes `
                -ResourceName $websiteName/$instanceName `
                -ApiVersion 2018-02-01 `
                -ErrorAction Ignore
            }
        catch { continue }

        foreach($process in $processes) {
            $processId = $process.Properties.id
            
            try {
                $processDetails = Get-AzResource -ResourceGroupName $resoureGroupName `
                    -ResourceType Microsoft.Web/sites/instances/processes `
                    -ResourceName $websiteName/$instanceName/$processId `
                    -ApiVersion 2018-02-01 `
                    -ErrorAction Ignore
                
                if ($processDetails.Properties.environment_variables.COMPUTERNAME -ne $null) {
                    Write-Host $websiteName " : " $processDetails.Properties.environment_variables.COMPUTERNAME
                }
            } catch { }
       }
    }
} 

Now I can see the app services that those servers belong to:

Shout out to Prashant Pratap for the inspiration from his AzureRm script!

Hope this is helpful to someone out there.

~David

Add comment

Loading