You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure Graph查询Azure租户新建资源及虚拟机的问题

解决Azure资源查询空结果及筛选虚拟机问题

原查询返回空结果的常见原因及修正

  1. 时区不匹配:Azure资源的创建时间统一使用UTC格式,原代码用本地时间计算时间范围,会导致查询的起始时间偏差。修正时需先将当前时间转为UTC再计算过去48小时。
  2. 创建时间字段覆盖不全:部分Azure资源的创建时间字段命名为properties.creationTime,原查询未包含该字段,需补充到coalesce函数中。
  3. 权限或订阅范围限制:确保你的账号拥有查询目标订阅资源的权限,若需指定特定订阅,可在Search-AzGraph命令中添加-Subscription参数。

修改后的查询代码(仅返回过去48小时内创建的虚拟机)

# 连接Azure账号
Connect-AzAccount

# 定义UTC时区的时间范围(过去48小时)
$startTime = (Get-Date).ToUniversalTime().AddHours(-48).ToString("yyyy-MM-ddTHH:mm:ssZ")

# 查询过去48小时内创建的虚拟机
$query = @"
Resources
| extend createdTime = coalesce(todatetime(properties.createdTime), todatetime(properties.createdOn), todatetime(properties.creationTime))
| where isnotnull(createdTime) and createdTime > datetime($startTime)
| where type == "Microsoft.Compute/virtualMachines"
| project name, location, resourceGroup, subscriptionId, createdTime
| order by createdTime desc
"@

# 执行查询(如需指定订阅,添加-Subscription 参数,示例:Search-AzGraph -Query $query -Subscription "订阅ID1", "订阅ID2")
$vmResources = Search-AzGraph -Query $query

内容的提问来源于stack exchange,提问作者learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:47:04