[英]PowerShell Array, adding new records where one property is an array
I am unable to use ArrayList or avoid using +=
for array manipulation.我无法使用 ArrayList 或避免使用
+=
进行数组操作。 Wishing that powerShell had a universal add or append available.希望 powerShell 有一个通用的 add 或 append 可用。
I have the below JSON array for $aksAppRules.RulesText
我有
$aksAppRules.RulesText
的以下 JSON 数组
[{
"Name": "A2B",
"Description": null,
"SourceAddresses": [
"10.124.176.0/21",
"10.124.184.0/21"
],
"TargetFqdns": [
"*.github.com",
"*.grafana.com",
"*.trafficmanager.net",
"*.loganalytics.io",
"*.applicationinsights.io",
"*.azurecr.io",
"*.debian.org"
],
"FqdnTags": [],
"Protocols": [
{
"ProtocolType": "Https",
"Port": 443
}
],
"SourceIpGroups": []
},
{
"Name": "Y2office365",
"Description": null,
"SourceAddresses": [
"10.124.176.0/21",
"10.124.184.0/21"
],
"TargetFqdns": [
"smtp.office365.com"
],
"FqdnTags": [],
"Protocols": [
{
"ProtocolType": "Http",
"Port": 25
},
{
"ProtocolType": "Http",
"Port": 587
}
],
"SourceIpGroups": []
}
]
I managed to make this work with the below powershell snippet我设法使用下面的 powershell 片段来完成这项工作
$new_list = @()
$collectionRules = $aksAppRules.RulesText | ConvertFrom-Json
foreach ($rule in $collectionRules) {
$protoArray = @()
ForEach ($protocol in $rule.Protocols) {
$protoArray += $protocol.ProtocolType + "," + $protocol.Port
}
#$new_list += , @($rule.Name, $rule.SourceAddresses, $rule.TargetFqdns, $protoArray )
# the 'comma' right after += in below line tells powershell to add new record.
$new_list += , @{Name=$rule.Name;SourceAddresses=$rule.SourceAddresses; TargetFqdns=$rule.TargetFqdns;Protocol=$protoArray}
}
$new_list | ConvertTo-Json | ConvertFrom-Json | select Name, SourceAddresses, TargetFqdns, Protocol| Convert-OutputForCSV -OutputPropertyType Comma | Export-Csv .\test.csv
The CSV looks like CSV 看起来像
I am unable to do this using Arraylists
and without using += as I heard it is inefficient with large arrays.我无法使用
Arraylists
并且不使用 += 来做到这一点,因为我听说大型 arrays 效率低下。
I have to copy things to a new array because I have to change the key:value format of the original "Protocols" to a 2 d array.我必须将内容复制到一个新数组,因为我必须将原始“协议”的键:值格式更改为二维数组。
Any pointers will be appreciated.任何指针将不胜感激。
Yes, you should avoid using the increase assignment operator (+=) to create a collection as it exponential expensive.是的,您应该避免使用增加赋值运算符 (+=) 来创建集合,因为它是指数级的昂贵。 Instead you should use the pipeline
相反,您应该使用管道
collectionRules = $aksAppRules.RulesText | ConvertFrom-Json
foreach ($rule in $collectionRules) {
[pscustomobject]@{
Name = $rule.Name
SourceAddresses = $rule.SourceAddresses
TargetFqdns = $rule.TargetFqdns
Protocol = @(
ForEach ($protocol in $rule.Protocols) {
$protocol.ProtocolType + "," + $protocol.Port
}
)
}
} | Convert-OutputForCSV -OutputPropertyType Comma | Export-Csv .\test.csv
Note 1 : I have no clue why you are doing | ConvertTo-Json | ConvertFrom-Json
注1 :我不知道你为什么这样做
| ConvertTo-Json | ConvertFrom-Json
| ConvertTo-Json | ConvertFrom-Json
| ConvertTo-Json | ConvertFrom-Json
, so far I can see there is no need for this if you use a [pscustomobject]
rather than a [Hashtabe]
type. | ConvertTo-Json | ConvertFrom-Json
,到目前为止,如果您使用[pscustomobject]
而不是[Hashtabe]
类型,我可以看到没有必要这样做。
Note 2 : I no clue what the function Convert-OutputForCSV
is doing and suspect that isn't required either (but left it in).注意 2 :我不知道 function
Convert-OutputForCSV
在做什么,并且怀疑这也不是必需的(但把它留在里面)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.