powershell - 从 powershell 模块运行应用程序洞察查询

标签 powershell azure-functions azure-cli

使用 AZ Cli,我可以运行以下命令来查询应用程序洞察实例:

az 监视器应用见解查询

azure powershell 模块中似乎没有直接等效项。我看到建议您应该使用 REST API 来获取应用程序洞察,但这需要 API key 。

我没有每个(许多)应用程序见解的 API key ,并且不想创建和存储它们,以便我可以查询应用程序见解 - 无法在以下环境中使用 Az Cli 的原因我的脚本是我想将我的脚本作为函数应用程序运行,并且函数应用程序不支持 az cli。

有没有一种我缺少的从 powershell 查询 AI 的替代方法?

最佳答案

目前,Azure PowerShell 仅提供管理 Azure 应用程序洞察资源的模块。更多详情请引用here 。因此,如果要使用PowerShell查询应用程序洞察,我们需要使用rest API。此外,如果您不想使用 API key 访问它,您可以使用 AD token 来访问它。更多详情请引用here

例如

如果您想通过Azure AD身份验证访问Azure application Insights API,请引用以下步骤

  1. 在租户中注册 Azure AD 应用程序

  2. 配置 API 权限 enter image description here enter image description here

  3. Create a client secret for the application

  4. 配置将贡献者分配给订阅中的 AD 应用程序

  5. 脚本

$appKey=""
$appId=""
$resource="https://api5.applicationinsights.io"
$secpasswd = ConvertTo-SecureString $appKey -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($appId, $secpasswd)

Connect-AzAccount -ServicePrincipal -Tenant "hanxia.onmicrosoft.com" -Credential $mycreds
$res=Get-AzAccessToken -ResourceUrl $resource


$headers=@{"Authorization"="Bearer "+$res.Token}
$body=@{"timespan"="P7D"; "query"="requests| summarize totalCount=sum(itemCount) by bin(timestamp, 30m)"}| ConvertTo-Json

Invoke-RestMethod 'https://api.applicationinsights.io/v1/apps/bd7cacd8-9607-4b53-b57b-995255292f36/query' -Method 'POST' -Headers $headers -Body $body -ContentType "application/json"

关于powershell - 从 powershell 模块运行应用程序洞察查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67808611/

相关文章:

powershell - Powershell Write-host将变量中的参数输出为字符串

Azure CLI 任务不适用于 Windows 构建代理

azure - 如何从“访问控制”选项卡获取存储帐户容器的角色分配

r - 用于运行 R 脚本的 Azure ML 与 Azure 函数

Azure CLI - az 存储 blob 批量删除模式

powershell - 删除脚本中的别名

powershell - 运行 PowerShell 时强制执行 ErrorActionPreference?

powershell - 如何让详细消息出现在 Start-Transcript 的输出中?

c# - 耐用功能与遥测无关

azure - 如何在 Azure 中使用事件网格触发 C++ 代码?