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
  • 11
  • 2

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
  • 14,879
  • 7
  • 40
  • 67