2

I'm trying to parse Palo Alto firewall rules with Powershell. Some rules can have ~100 servernames in them. I need to break down each name to check if they're still alive (DNS) and return rule mapping for each servername. Most of it works except for:

Currently I have:

RuleName, {Hostname1;Hostname2;Hostname3}

I'm trying to get:

RuleName, Hostname1 ;
RuleName, Hostname2 ;
RuleName, Hostname3

I started with a hashtable, but having issues with overall script length - too long

I've been doing:

Select-Object RuleName,@{Name="Hostnames";Expression={($_.Hostnames).Split(";")}

But feels like I need a ForEach-Object in the Expression to add the RuleName field to each hostname, so it doesn't turn into a collection.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
El8Hax0r
  • 21
  • 4

1 Answers1

0

You need to invert your logic: enumerate the hostnames first, then construct an object for each:

foreach ($host in $_.Hostnames.Split(';')) {
  [pscustomobject] @{
    RuleName = $_.RuleName
    Hostname = $host
  }
}

Note that in this case it's simpler to use a [pscustomobject] literal to construct the output objects, although a Select-Object solution with calculated properties (as in your attempt), is possible too:

$thisObj = $_
$thisObj.Hostnames.Split(';') |
  Select-Object @{ Name='RuleName'; Expression={ $thisObj.RuleName } }, 
                @{ Name='Hostname'; Expression={ $_ } }
mklement0
  • 382,024
  • 64
  • 607
  • 775