Script/Azure query to get instrumentation key of application insights from the configuration of multiple app services

Manish Shinde 0 Reputation points
2024-02-14T16:36:27.0133333+00:00

Script/Azure query to get instrumentation key of application insights from the configuration of multiple app services under resources group from different subscription.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,310 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,888 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Achraf Ben Alaya 1,056 Reputation points MVP
    2024-02-14T16:47:08.37+00:00

    To retrieve the instrumentation key of Application Insights from the configuration of multiple app services under resources group from different subscriptions using Azure CLI script, you can use the following steps:

    1. Loop through each subscription.
    2. List all resource groups in each subscription.
    3. Filter resource groups based on your criteria (e.g., by name).
    4. List all app services within each resource group.
    5. For each app service, check if it's using Application Insights.
    6. If Application Insights is configured, retrieve the instrumentation key. Here's a sample Azure CLI script to achieve this: bash
    #!/bin/bash
    # Azure subscriptions array
    subscriptions=("subscription_id1" "subscription_id2" "subscription_id3")
    # Loop through each subscription
    for subscription in "${subscriptions[@]}"
    do
        echo "Subscription: $subscription"
        # Set the current subscription
        az account set --subscription "$subscription"
        # List all resource groups
        resourceGroups=$(az group list --query "[].name" -o tsv)
        # Loop through each resource group
        for rg in $resourceGroups
        do
            echo "Resource Group: $rg"
            # List all app services in the resource group
            appServices=$(az webapp list --resource-group $rg --query "[].name" -o tsv)
            # Loop through each app service
            for appService in $appServices
            do
                echo "App Service: $appService"
                # Check if Application Insights is enabled for the app service
                aiEnabled=$(az webapp config show --name $appService --resource-group $rg --query "applicationInsights.name" -o tsv)
                if [[ -n $aiEnabled ]]; then
                    # If Application Insights is enabled, retrieve the instrumentation key
                    instrumentationKey=$(az monitor app-insights component show --app $aiEnabled --query "instrumentationKey" -o tsv)
                    echo "Instrumentation Key: $instrumentationKey"
                else
                    echo "Application Insights not configured for $appService"
                fi
            done
        done
    done
    

    Replace subscription_id1, subscription_id2, etc., with your actual subscription IDs. This script assumes that you have the necessary permissions to access the subscriptions, resource groups, and app services, and also assumes that Application Insights is used for monitoring the app services. Adjust the script as needed based on your specific requirements and environment.


  2. Ryan Hill 28,386 Reputation points Microsoft Employee
    2024-02-15T23:35:06.73+00:00

    @Manish Shinde you can use the following script

    $resourceGroupNames = @("myResourceGroup1", "myResourceGroup2", "myResourceGroup3")
    foreach ($resourceGroupName in $resourceGroupNames)
    {
        Write-Output "Resource Group: $resourceGroupName"
        $webApps = Get-AzWebApp -ResourceGroupName $resourceGroupName
        foreach ($webApp in $webApps)
        {
            $appInsightsKey = $webApp.SiteConfig.AppSettings | Where-Object {$_.Name -eq "APPINSIGHTS_INSTRUMENTATIONKEY"} | Select-Object -ExpandProperty Value
            Write-Output "`t$($webApp.Name): $appInsightsKey"
        }
    }
    
    0 comments No comments

  3. Pinaki Ghatak 4,610 Reputation points Microsoft Employee
    2024-02-16T09:10:25.0633333+00:00

    Hello @Manish Shinde

    Since you want to span over multiple subscriptions, and then many resource groups within a subscription, you could write something like this:

    # Import the required module
    Import-Module Az
    
    # Login to Azure
    Connect-AzAccount
    
    # Get the list of subscriptions
    $subscriptions = Get-AzSubscription
    
    # Iterate over each subscription
    foreach ($subscription in $subscriptions) {
        # Set the current subscription context
        Set-AzContext -Subscription $subscription.Id
    
        # Get the list of resource groups in the current subscription
        $resourceGroups = Get-AzResourceGroup
    
        # Iterate over each resource group
        foreach ($resourceGroup in $resourceGroups) {
            # Get the list of App Services in the current resource group
            $appServices = Get-AzWebApp -ResourceGroupName $resourceGroup.ResourceGroupName
    
            # Iterate over each App Service
            foreach ($appService in $appServices) {
                # Get the Application Insights component linked to the current App Service
                $appInsightsComponent = Get-AzResource -Id (Get-AzWebAppSetting -Name $appService.Name -ResourceGroupName $resourceGroup.ResourceGroupName).Properties.APPINSIGHTS_INSTRUMENTATIONKEY
    
                if ($null -ne $appInsightsComponent) {
                    # Get the instrumentation key of the Application Insights component
                    $instrumentationKey = (Get-AzApplicationInsights -ResourceGroupName $appInsightsComponent.ResourceGroupName -Name $appInsightsComponent.Name).InstrumentationKey
    
                    Write-Output "Subscription: $($subscription.Name), Resource Group: $($resourceGroup.ResourceGroupName), App Service: $($appService.Name), Application Insights: $($appInsightsComponent.Name), Instrumentation Key: $instrumentationKey"
                }
            }
        }
    }
    
    

    Please replace "Your Subscription ID" with your actual Azure subscription ID. This script iterates over all subscriptions, resource groups, and app services, and prints out the subscription name, resource group name, app service name, linked Application Insights component, and its instrumentation key. Here you could do your operations as you like.

    This script assumes that the APPINSIGHTS_INSTRUMENTATIONKEY app setting in your App Service configuration contains the resource ID of the linked Application Insights component. If your setup is different, you may need to adjust the script accordingly.


    If this information provided here helps solve your issue, please tag this as answered, so it helps further community readers, who may have similar questions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.