代码之家  ›  专栏  ›  技术社区  ›  mark

如何以非交互方式从Powershell脚本运行Azure日志分析查询?

  •  0
  • mark  · 技术社区  · 6 年前

    鉴于:

    • 我有一个Azure帐户(MSDN福利)。
    • 我有一个控制台应用程序向我的AppInsights工作区发送自定义的AppInsights度量。

    我想从PowerShell脚本中查询这些指标。

    我确实试着通过谷歌搜索找到一个解决方案——没有成功。并不是说没有关于这个主题的帖子-我只是无法让它工作以下这些帖子。

    问题的要点是如何在没有用户交互的情况下实现。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Ivan Glasenberg    6 年前

    步骤1:获取应用程序ID和API密钥。

    导航到您的应用程序见解->API访问,请参见屏幕截图(请记住,生成API密钥时,请将其写下来): enter image description here

    Invoke-WebRequest -Uri https://api.applicationinsights.io/v1/apps/your_application_id/metrics/customEvents/cou
    nt?timespan=P20D -Headers @{"accept"="application/json"; "x-api-key"="your_api_key"}
    

    结果如下: enter image description here

    here .

        2
  •  4
  •   Tatu Lahtela    5 年前

    application-insights 扩展到az cli。

    az extension add -n application-insights
    az monitor app-insights query --apps "$my-app-name" --resource-group "$my-resource-group" --offset 24H --analytics-query 'requests | summarize count() by bin(timestamp, 1h)'
    

    下面是一个powershell脚本,可以从给定的application insight实例和资源组中的文件运行kusto查询,并将数据作为powershell表返回:

    <#
    .SYNOPSIS
    
    Run query in application insights and return Powershell table
    
    .PARAMETER filename
    
    File name of kusto query
    
    .PARAMETER app 
    
    Application Insights instance name
    
    .PARAMETER rg
    
    Resource group name
    
    .EXAMPLE
    
    Search-AppInsights -filename file.kusto -app my-app-name -rg my-resource-group-name
    
    #>
    param([string] $filename, [string]$app, [string]$rg)
    
    $query = Get-Content $filename
    $data = az monitor app-insights query --apps "$app" --resource-group "$rg" --offset 48H --analytics-query "$query" | ConvertFrom-Json
    $cols = $data.tables.columns | % {  $_.name }
    $data.tables.rows | % {
        $obj = New-Object -TypeName psobject
        for ($i=0; $i -lt $cols.Length; $i++) {
        $obj | Add-Member -MemberType NoteProperty -Name $cols[$i] -Value $_[$i]
        }
        $obj
    }
    
        3
  •  3
  •   Felix Bodmer    5 年前
    $WorkspaceName = 'weu-co-law-security-01'
    $ResourceGroupName = 'aaa-co-rsg-security-01'
    $Workspace = Get-AzOperationalInsightsWorkspace -ResourceGroupName $ResourceGroupName -Name $WorkspaceName
    $QueryResults = Invoke-AzOperationalInsightsQuery -Workspace $Workspace -Query 'AuditLogs | where OperationName == "Add member to group" | project TargetResources[0].displayName'
    $QueryResults.Results