【Azure 环境】Azure Resource Graph Explorer 中实现动态数组数据转换成多行记录模式 - mv-expand
问题描述
想对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.) , 它可以将多值动态数组或属性包扩展为多个记录。
如把值
1, [10,20]
2, [a,b]
两行数据转换4行数据:
1, 10
1, 20
2, a
2, b
详细的解说可以参考MV-EXPAND文档(https://docs.microsoft.com/zh-cn/azure/data-explorer/kusto/query/mvexpandoperator#examples)。

回归到本文问题解答,是列举出NSG Rule。
首先,通过Resource Graph Explorer获取到全部的NSG信息:
resources
| where type =~ "microsoft.network/networksecuritygroups"
因为NSG Rules的结果是JSON数组格式,所以需要对它进行转换(MV-EXPAND)。然后,通过extend方式获取到Properties中的属性值:
resources
| where type =~ "microsoft.network/networksecuritygroups" and subscriptionId =="a9dc7515-7692-4316-9ad4-762f383eec10"
|mv-expand rules=properties.securityRules
|extend direction=tostring(rules.properties.direction)
|extend priority=toint(rules.properties.priority)
|extend rule_name = rules.name
|extend nsg_name = name
|extend description=rules.properties.description
|extend destination_prefix=iif(rules.properties.destinationAddressPrefixes=='[]', rules.properties.destinationAddressPrefix, strcat_array(rules.properties.destinationAddressPrefixes, ","))
|extend destination_asgs=iif(isempty(rules.properties.destinationApplicationSecurityGroups), '', strcat_array(parse_json(rules.properties.destinationApplicationSecurityGroups), ","))
|extend destination=iif(isempty(destination_asgs), destination_prefix, destination_asgs)
|extend destination=iif(destination=='*', "Any", destination)
|extend destination_port=iif(isempty(rules.properties.destinationPortRange), strcat_array(rules.properties.destinationPortRanges,","), rules.properties.destinationPortRange)
|extend source_prefix=iif(rules.properties.sourceAddressPrefixes=='[]', rules.properties.sourceAddressPrefix, strcat_array(rules.properties.sourceAddressPrefixes, ","))
|extend source_asgs=iif(isempty(rules.properties.sourceApplicationSecurityGroups), "", strcat_array(parse_json(rules.properties.sourceApplicationSecurityGroups), ","))
|extend source=iif(isempty(source_asgs), source_prefix, tostring(source_asgs))
|extend source=iif(source=='*', 'Any', source)
|extend source_port=iif(isempty(rules.properties.sourcePortRange), strcat_array(rules.properties.sourcePortRanges,","), rules.properties.sourcePortRange)
|extend action=rules.properties.access
|extend subnets = strcat_array(properties.subnets, ",")
|project resourceGroup, nsg_name, rule_name, subnets, direction, priority, action, source, source_port, destination, destination_port, description, subscriptionId, id
|sort by resourceGroup asc, nsg_name, direction asc, priority asc
获取到的结果为:

######################################################################################################
附录一 : NSG Rules 中 securityRules 的格式为:
"securityRules": [
{
"properties": {
"provisioningState": "Succeeded",
"destinationAddressPrefixes": [],
"destinationAddressPrefix": "VirtualNetwork",
"sourceAddressPrefixes": [],
"destinationPortRanges": [],
"destinationPortRange": "*",
"sourceAddressPrefix": "VirtualNetwork",
"sourcePortRanges": [],
"sourcePortRange": "*",
"priority": 65000,
"protocol": "*",
"direction": "Inbound",
"access": "Allow",
"description": "Allow inbound traffic from all VMs in VNET"
},
"id": ““
},
… …
{
"properties": {
"provisioningState": "Succeeded",
"destinationAddressPrefixes": [],
"destinationAddressPrefix": "*",
"sourceAddressPrefixes": [],
"destinationPortRanges": [],
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"sourcePortRanges": [],
"sourcePortRange": "*",
"priority": 65500,
"protocol": "*",
"direction": "Outbound",
"access": "Deny",
"description": "Deny all outbound traffic"
},
"id": "/“
}
]
参考资料
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
【Azure 环境】Azure Resource Graph Explorer 中实现动态数组数据转换成多行记录模式 - mv-expand的更多相关文章
- 【Azure Developer】在Azure Resource Graph Explorer中查看当前订阅下的所有资源信息列表并导出(如VM的名称,IP地址内网/公网,OS,区域等)
问题描述 通过Azure的Resource Graph Explorer(https://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade),可以查 ...
- (待续)C#语言中的动态数组(ArrayList)模拟常用页面置换算法(FIFO、LRU、Optimal)
目录 00 简介 01 算法概述 02 公用方法与变量解释 03 先进先出置换算法(FIFO) 04 最近最久未使用(LRU)算法 05 最佳置换算法(OPT) 00 简介 页面置换算法主要是记录内存 ...
- 关于C#中的动态数组ArrayList
在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组. 一.ArrayList类与Array类的区别 ArrayList类实 ...
- [C] 在 C 语言编程中实现动态数组对象
对于习惯使用高级语言编程的人来说,使用 C 语言编程最头痛的问题之一就是在使用数组需要事先确定数组长度. C 语言本身不提供动态数组这种数据结构,本文将演示如何在 C 语言编程中实现一种对象来作为动态 ...
- 【Azure 环境】在Windows系统中 使用Terraform创建中国区Azure资源步骤(入门级)
Terraform(全称:Hashicorp Terraform )是一种开源工具,用于预配和管理云基础结构. 它将基础结构编入描述云资源拓扑的配置文件中. 这些资源包括虚拟机.存储帐户和网络接口等. ...
- 关于delphi XE7中的动态数组和并行编程(第一部分)
本文引自:http://www.danieleteti.it/category/embarcadero/delphi-xe7-embarcadero/ 并行编程库是delphi XE7中引进的最受期待 ...
- mysql中单个字段包含','转换成多条记录
问题:把value中的值取出作为另外一个表的where条件时,必须把value中的用','分隔的每一个id截取出来 解决方法: ),) ) #本文参考自网络某文章,非原创
- asp中的动态数组
<% Dim array1(),i ReDim array1(3)array1(3)=10response.Write(array1(3)&"<br>") ...
- 在PHP中如何把数组写成配置文件
1.配置文件 <?php return array ( 556770 => '65460d6684dcad3d0a92f1feb7fde199', 567701 => '9c2acd ...
随机推荐
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
1.前言 今天的理性认识主要就是讲解和分享Fiddler的一些理论基础知识.其实这部分也没有什么,主要是给小伙伴或者童鞋们讲一些实际工作中的场景,然后隆重推出我们的猪脚(主角)-Fiddler. 1. ...
- FFT 学习笔记(自认为详细)
引入 什么是 \(\text{FFT}\) ? 反正我看到 \(\text{wiki}\) 上是一堆奇怪的东西. 快速傅里叶变换(英语:Fast Fourier Transform, FFT),是快速 ...
- 基于InsightFace的高精度人脸识别,可直接对标虹软
一.InsightFace简介 InsightFace 是一个 2D/3D 人脸分析项目.InsightFace 的代码是在 MIT 许可下发布的. 对于 acadmic 和商业用途没有限制. 包含注 ...
- 方法的调用和JDK9的JShell简单使用
方法在定义完毕后,方法不会自己运行,必须被调用才能执行,我们可以在主方法main中来调用我们自己定义好的方法.在主方法中,直接写要调用的方法名字就可以调用了 public static void ma ...
- 【一知半解】synchronied
synchronized是什么 synchronized是java同步锁,同一时刻多个线程对同一资源进行修改时,能够保证同一时刻只有一个线程获取到资源并对其进行修改,因此保证了线程安全性. synch ...
- CTO与CIO选型数据中台的几大建议
企业数字化转型离不开企业数字化技术的配备.但企业在选择数字化技术时也面临着一个问题,就是如何在大胆采用先进的数字化技术和对技术进行投资之间找到平衡,将投资风险降到最低,毕竟错误的技术选型会给企业带来不 ...
- Solution -「Luogu 4135」作诗
写在前面 & 前置芝士 好像是好久没有打理 blog 了.感觉上学期是有点颓.嘶,初三了好好冲一次吧. 那么回到这道题目.你会分块就能看懂. 题目大意 先挂个来自洛谷的 link. ...
- logstash在windows系统下的安装与使用
前言: Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到 Elasticsearch. ES官网:https://www.elastic.co/p ...
- DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
随着企业规模的扩大,对数据库可用性要求越来越高,更多企业采用两地三中心.异地多活的架构,以提高数据库的异常事件应对能力. 在数据库领域,我们常听的"两地三中心"."异地多 ...
- DP の 百题大过关(5/100)
动态规划自古以来是DALAO凌虐萌新的分水岭,但有些OIer认为并没有这么重要--会打暴力,大不了记忆化.但是其实,动态规划学得好不好,可以彰显出一个OIerOIer的基本素养--能否富有逻辑地思 ...