Azure资源图查询:如何排除带pvc-前缀的磁盘资源?
解决Azure Resource Graph排除AKS PVC磁盘的查询问题
你遇到的核心问题是Kusto查询语言中!=不支持通配符匹配,所以name != "pvc-*"无法生效。要排除名称格式为pvc-[some_guid]的AKS PVC磁盘,有两种可行方案:
方案1:排除所有以pvc-开头的磁盘
使用startswith函数判断名称前缀并取反,实现简单高效的过滤:
resources | where type has "microsoft.compute/disks" | extend diskState = tostring(properties.diskState) | where diskState == 'Unattached' | where not name startswith "pvc-" // 替换原无效的通配符判断逻辑 | join kind=inner ( resourcecontainers | where type == 'microsoft.resources/subscriptions' | project subscriptionId, subscriptionName = name) on subscriptionId | project name, diskState, managedBy, subscriptionName, resourceGroup, location
方案2:精确匹配pvc-[GUID]格式的磁盘
如果需要避免误排除非PVC的pvc-开头磁盘,可通过正则表达式严格匹配GUID格式:
resources | where type has "microsoft.compute/disks" | extend diskState = tostring(properties.diskState) | where diskState == 'Unattached' | where not name matches regex @"^pvc-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" // 精准匹配PVC+标准GUID格式 | join kind=inner ( resourcecontainers | where type == 'microsoft.resources/subscriptions' | project subscriptionId, subscriptionName = name) on subscriptionId | project name, diskState, managedBy, subscriptionName, resourceGroup, location
原理说明
Kusto中==/!=仅支持精确字符串匹配,通配符*不能直接用于这类比较操作。要实现模糊或格式匹配,需使用专门的字符串处理函数:
startswith:快速判断字符串前缀matches regex:通过正则表达式实现复杂格式校验
内容的提问来源于stack exchange,提问作者NickP
相关产品推荐
相关产品推荐

