Search code examples
azure-application-insightsazure-powershellkqlazure-log-analyticsazure-monitoring

How can a user run Query Pack items on Application Insights using PowerShell?


Using the steps below, one can run a Kusto query stored in a Default Query Pack from PowerShell on a Log Analytics Workspace.

$baseUrl  = "https://management.azure.com"
$RId      = (Get-AzResource -ResourceGroupName LogAnalyticsDefaultResources -Name DefaultQueryPack).ResourceId
$token    = (Get-AzAccessToken -ResourceUrl $baseUrl).Token
$response = Invoke-RestMethod -Uri "$baseUrl$RId/queries?api-version=2019-09-01" -Method Get -Headers @{Authorization = "Bearer $token"}

$response.value.properties.displayname  # Display names
#Runs the first query in the pack
Invoke-AzOperationalInsightsQuery -Query ($response.value)[0].properties.body -WorkspaceId <>

How should a user run a query from the Default pack on Application Insights connected to the Log Analytics Workspace using Invoke-AzOperationalInsightsQuery? I get Operation returned an invalid status code 'BadRequest' while running an AppInsights query.


Solution

  • After converting one's classic AppInsights resource and connecting it with a Log Analytics Workspace one can update one's AppInsights queries based on the new table structure.

    Migrate an Application Insights classic resource to a workspace-based resource

    Once the AppInsights query is updated to use the new table structure, one can save them to the Default query pack and use the same method to execute it from PowerShell.

    For Example: The AppInsights query pageViews| take 10 would become AppPageViews| take 10.