如何查找关联特定Azure App Insights的所有资源?
查找关联特定Application Insights的Azure资源方案
因为不同Azure资源存储Application Insights(AI)配置的位置不同,你之前的Graph查询仅匹配顶层ConnectionString字段,所以会遗漏大部分资源,以下是几种更可靠的方案:
1. 优化Azure Resource Graph查询(覆盖多资源类型)
针对常见资源类型(Function App、App Service、VM、Logic Apps等),分别匹配其存储AI配置的字段(Instrumentation Key或连接字符串),Kusto查询示例:
Resources | where type in ( "microsoft.web/sites", "microsoft.functionapps/sites", "microsoft.compute/virtualmachines", "microsoft.logic/workflows" ) // 解析App Service/Function App的应用设置 | extend appSettings = parse_json(properties.siteConfig.appSettings) | extend aiKey = tostring(appSettings.APPINSIGHTS_INSTRUMENTATIONKEY) | extend aiConnString = tostring(appSettings.APPLICATIONINSIGHTS_CONNECTION_STRING) // 解析VM的Azure Monitor扩展配置 | extend aiExtension = parse_json(properties.extensions) | extend vmAiKey = tostring(aiExtension["Microsoft.Insights.AzureMonitorAgent"]?.settings?.instrumentationKey) // 匹配目标AI的标识(替换成你的AI密钥或连接字符串) | where aiKey == "<你的AI Instrumentation Key>" or aiConnString contains "<你的AI连接字符串>" or vmAiKey == "<你的AI Instrumentation Key>" | project 资源名称=name, 资源类型=type, 资源组=resourceGroup, AI密钥=aiKey, AI连接字符串=aiConnString
执行命令:
az graph query -q "上面的Kusto查询内容"
2. Azure CLI批量检查特定资源类型
如果只关注某一类资源(比如所有Function App),可以用CLI批量拉取配置并匹配:
批量检查Function App
# 遍历所有Function App,检查应用设置中的AI配置 az functionapp list --query "[].{name:name, resourceGroup:resourceGroup}" -o tsv | while read name rg; do settings=$(az functionapp config appsettings list --name $name --resource-group $rg --query "[?name in ('APPINSIGHTS_INSTRUMENTATIONKEY', 'APPLICATIONINSIGHTS_CONNECTION_STRING')].value" -o tsv) if echo "$settings" | grep -q "<你的AI密钥或连接字符串>"; then echo "匹配到:Function App $name (资源组:$rg)" fi done
批量检查App Service
把命令中的functionapp替换为webapp即可。
3. 门户辅助查看关联资源
在Azure门户打开目标Application Insights资源,左侧菜单选择「关联的资源」,这里会显示通过官方集成(比如创建资源时直接关联)配置的资源,但手动添加密钥的资源不会显示,仅作为补充手段。
没有单一方案能覆盖所有场景,建议组合使用上述方法,确保不遗漏关联资源。
内容的提问来源于stack exchange,提问作者Harry
相关产品推荐
相关产品推荐

