如何创建可打开Log Analytics并运行指定查询的URL?遇格式错误求解决方案
生成Log Analytics直接运行指定查询的URL方案
核心要求说明
Log Analytics的直接运行查询URL中,query参数必须是GZIP压缩后再经过URL安全Base64编码的字符串,这是你之前尝试普通编码失败的原因。以下是仅用KQL实现编码并生成可用URL的步骤:
步骤1:用KQL生成符合要求的查询编码字符串
假设你要预定义的查询是AppRequests | take 5,运行以下KQL代码,即可得到编码后的字符串:
let targetQuery = "AppRequests | take 5"; // 替换成你需要的目标查询 let compressedBytes = zip(pack_string(targetQuery)); let base64Encoded = tobase64(compressedBytes); let urlSafeBase64 = replace_string(replace_string(base64Encoded, "+", "-"), "/", "_"); let finalQueryParam = replace_string(urlSafeBase64, "=", ""); print finalQueryParam
代码逻辑说明:
pack_string:将查询字符串转换为字节数组zip:对字节数组执行GZIP压缩tobase64:将压缩后的字节转为Base64字符串- 两次
replace_string:将Base64中的+替换为-、/替换为_,去掉末尾的=,生成URL安全的编码结果
步骤2:拼接最终可运行的URL
将步骤1得到的finalQueryParam,替换到修正后的URL模板中(注意修正原URL的拼写错误:resoruceGroup→resourceGroup,workspceName→workspaceName):
https://portal.azure.com/#blade/Microsoft_OperationsManagementSuite_Workspace/AnalyticsBlade/initiator/AnalyticsShareLinkToQuery/isQueryEditorVisible/true/scope/resources/resourceId/subscriptions/{subscriptionId}/resourcegroups/{resourceGroup}/providers/microsoft.operationalinsights/workspaces/{workspaceName}/query/{finalQueryParam}/isQueryBase64Compressed/true
替换模板中的{subscriptionId}、{resourceGroup}、{workspaceName}为实际值后,即可直接打开该URL运行预定义查询。
注意事项
- 若目标查询包含双引号,需用双引号转义,例如查询为
where name == "test"时,KQL中应写为let targetQuery = "where name == ""test"""; isQueryBase64Compressed参数必须保持为true,否则门户无法识别压缩后的查询内容
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

