azure-powershell - 将 Log Analytics 工作区查询的结果输出到事件中心
问题描述
我正在执行查询以输出在 Azure Log Analytics 工作区中捕获的日志,例如: Invoke-AzOperationalInsightsQuery -WorkspaceId '' -Query "AzureDiagnostics | where Category == 'AzureFirewallApplicationRule'"
但是,我需要将此结果发送到事件中心以进行进一步处理。
我正在尝试使用 REST API ( https://docs.microsoft.com/en-us/rest/api/eventhub/send-batch-events ) 但努力动态生成请求正文以发送到事件中心基于查询的输出字段。这可能不是最好的方法,有什么建议吗?
解决方案
我建议你可以通过一个一个发送一个简单的json数据来使用Send event api。因为如果你使用发送批处理 api,你应该构建一个更复杂的源数据。
您可以使用以下 powershell 代码通过发送事件api 将数据发送到事件中心。
$queryResults = Invoke-AzOperationalInsightsQuery -WorkspaceId "xxx" -Query "your query"
#generate sas token
$URI_1 = "event_hub_namespace.servicebus.windows.net/eventhub_path"
$Access_Policy_Name="RootManageSharedAccessKey"
$Access_Policy_Key="the key"
#Token expires now+3000
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+3000
$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI_1)+ "`n" + [string]$Expires
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
$Signature = [Convert]::ToBase64String($Signature)
$SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI_1) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name
$SASToken
$method = "POST"
$url = "https://event_hub_namespace.servicebus.windows.net/eventhub_path/messages"
$signature = $SASToken
# API headers
$headers = @{
"Authorization"=$signature;
"Content-Type"="application/atom+xml;type=entry;charset=utf-8";
}
#use foreach to send data
foreach($s in $queryResults.Results){
#Write-Output "hello"
$json = $s | ConvertTo-Json
#Write-Output $json
Invoke-WebRequest -Method $method -Headers $headers -Body $json -uri $url
}
Write-Output "**completed**"
执行 powershell后,我使用代码从事件中心接收数据,我可以确认所有数据都发送到事件中心。截图如下:
推荐阅读
- glib - 在使用较旧和较新的 GLib 版本时将 g_autoptr() 与 G_DEFINE_AUTOPTR_CLEANUP_FUNC() 一起使用
- android - '未来的实例
, 但期望 bool - python - Python 添加到 Pandas 数据框替换信息
- javascript - Create-react-app:Safari iOS10 上标识符中的 unicode 转义无效
- anypoint-studio - 在 studio 版本中安装 Subversion 插件:7.4.2 失败
- kubernetes - Kubernetes - 在两个容器之间以及与外界进行通信的最佳方式
- spring-boot - Java RestCall 默认绕过用户凭据
- java - 避免对所有 http 请求进行冗余授权检查
- python - 熊猫应用函数抛出值错误
- objective-c - Xcode GoogleCast 与 CFBundleExecutable 在Archieve App 的问题