问题描述
想对Azure中全部VM的NSG资源进行收集,如果只是查看一个VM的NSG设定,可以在门户页面中查看表格模式,但是如果想把导出成表格,可以在Azure Resource Graph Explorer中查找到资源,但是,它的格式是JSON数组,在一行中显示,那么如何把一行数据中的数组转换成多行记录(提高可读性)呢?
VM NSG门户显示为表格模式:
Azure Resource Graph Explorer中显示为JSON 数组:
问题解答
在Azure Resource Graph Explorer 中,所使用的Kusto Query 语言中,有一个运算符 : mv-expand (Expands multi-value dynamic arrays or property bags into multiple records.) , 它可以将多值动态数组或属性包扩展为多个记录。
如把值
两行数据转换4行数据:
详细的解说可以参考MV-EXPAND文档(https://docs.microsoft.com/zh-cn/azure/data-explorer/kusto/query/mvexpandoperator#examples)。
回归到本文问题解答,是列举出NSG Rule。
首先,通过Resource Graph Explorer获取到全部的NSG信息:
因为NSG Rules的结果是JSON数组格式,所以需要对它进行转换(MV-EXPAND)。然后,通过extend方式获取到Properties中的属性值:
获取到的结果为:
######################################################################################################
附录一 : NSG Rules 中 securityRules 的格式为:
参考资料
mv-expand 运算符 : https://docs.microsoft.com/zh-cn/azure/data-explorer/kusto/query/mvexpandoperator
Azure Resource Graph Query For Network Security Group Rules : https://blog.tyang.org/2021/12/08/azure-resource-graph-query-for-nsg-rules
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!