Executing Azure KQL Query using PowerShell?

EnterpriseArchitect 6,041 Reputation points
2023-12-07T06:42:36.1266667+00:00

How can I use PowerShell to execute the Azure KQL query?

AzureActivity  
| where TimeGenerated >= ago(90d)  
| project  _ResourceId

Any help would be greatly appreciated.

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Graph
{count} votes

Accepted answer
  1. Luis Arias 8,621 Reputation points Volunteer Moderator
    2023-12-07T13:59:03.53+00:00

    Hello EnterpriseArchitect,

    You can use the bellow code to query your log analytics, first you need to login and setup the AzContext on powershell:

    Connect-AzAccount
    Set-AzContext -Subscription "<Your-Subscription>"
    $la_name = "<Your-LogAnalytics-name>"
    $la_rg = "<Loganalytics-RG>"
    $la_id = (Get-AzOperationalInsightsWorkspace -Name $la_name -ResourceGroupName $la_rg).CustomerID
    ## Take in consideration you have permission as reader over workspace
    $query = "AzureMetrics 
    | where TimeGenerated >= ago(1d)"
    
    $outQuery.Results | Out-GridView
    
    
    

    Let me know if this is helpful.

    Luis

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. george zaki 5 Reputation points
    2024-09-02T02:10:27.4333333+00:00

    Connect-AzAccount

    Set-AzContext -Subscription "<Your-Subscription>"

    $la_name = "<Your-LogAnalytics-name>"

    $la_rg = "<Loganalytics-RG>"

    $la_id = (Get-AzOperationalInsightsWorkspace -Name $la_name -ResourceGroupName $la_rg).CustomerID

    Take in consideration you have permission as reader over workspace

    $query = "AzureMetrics

    | where TimeGenerated >= ago(1d)"

    $outQuery.Results | Out-GridView

    0 comments No comments

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.