0

I am unable to use ArrayList or avoid using += for array manipulation. Wishing that powerShell had a universal add or append available.

I have the below JSON array for $aksAppRules.RulesText

    [{
    "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

$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


Output-formattedin Excel

I am unable to do this using Arraylists and without using += as I heard it is inefficient with large 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.

user587585
  • 21
  • 3

1 Answers1

0

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, so far I can see there is no need for this if you use a [pscustomobject] rather than a [Hashtabe] type.

Note 2: I no clue what the function Convert-OutputForCSV is doing and suspect that isn't required either (but left it in).

iRon
  • 20,463
  • 10
  • 53
  • 79
  • ConvertTo-Json|ConverFrom-json -- that worked for me.. don't remember reason. However the convert-OutputForCSV is a special function I found to workaround the issue of "system.object" that shows up... I went to modifying this using sed/printf and JQ using linux. I liked the "Properties" thing in powershell initially, but find that it is misleading and basic things like not writing the output value to an export is tiresome to keep up. – user587585 Nov 08 '22 at 01:14
  • @user587585, the `"system.object"` is probably caused by the default depth of `ConvertTo-Json`. See:[Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2](https://stackoverflow.com/q/53583677/1701026). – iRon Nov 08 '22 at 06:43