如何通过Azure Resource Graph的Kusto查询获取VM Agent版本?
解决方案
Azure Resource Graph的resources表默认仅返回VM的基础信息,完整的InstanceView数据(包括VM Agent版本)并未直接包含在该表中,可通过以下两种方式获取目标数据:
方法一:在Kusto查询中调用VM InstanceView API
借助invoke操作调用Azure REST API,拉取每个VM的完整InstanceView并提取Agent版本:
resources | where type == "microsoft.compute/virtualmachines" | project id, name, resourceGroup, subscriptionId | invoke azure_rest_api_request('/providers/Microsoft.Compute/virtualMachines/' + name + '/instanceView?api-version=2023-07-01') | extend instanceView = parse_json(response_body) | project VMName = name, ResourceGroup = resourceGroup, VMAgentVersion = instanceView.vmAgent.vmAgentVersion
注意:执行此查询需要账号拥有目标VM的
Microsoft.Compute/virtualMachines/instanceView/read权限,且API调用存在速率限制。
方法二:通过Azure Monitor Logs关联数据(需启用VM Insights)
若VM已启用VM Insights,可直接从VMComputer表提取Agent版本并与资源表关联:
VMComputer | project Computer, VMId, AgentVersion = tostring(Properties["azuremonitoragent.version"]) | join kind=inner ( resources | where type == "microsoft.compute/virtualmachines" | project VMId = id, VMName = name, ResourceGroup = resourceGroup ) on VMId | project VMName, ResourceGroup, AgentVersion
注意:未启用VM Insights的VM无法通过此方法返回数据。
内容的提问来源于stack exchange,提问作者kent2004
相关产品推荐
相关产品推荐

