I have an array that is full of firewall rules objects. These objects all have properties associated with firewall rules. Many of these properties are multi-value, for example the Source IP field could have multiple IP Addresses and Groups. The source zone the same. The same with Destination Zone, Destination IP, Service, Application, etc…
With that said, how can I show all the expanded results of all my multi-value properties on the console? How about when exporting to a text file? How about when exporting to a CSV?
How exactly do you need the output ? For example, do you want to have a single source IP field, and then if an object has multiple Source IPs each one gets listed on it’s own line? Or would fields like SourceIP1, SourceIP2… SourceIPN format work?
Since you can’t do an -expandproperty on multiple properties, the only thing I can think of is to iterate the object (essentially an array of arrays), and “flatten” it out, storing that in a new object, and exporting that.
Here’s a simple example, the dependentservices property could contain 0 or more services, I just took 3 as an example.
$Services = get-service CryptSvc, BITS
function Expand-DependentServices
{
[OutputType([PSObject])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipeline = $true,
Position=0)]
$service
)
Process
{
$name = $service.name
$depends = $service | select -ExpandProperty dependentservices
if ($depends){
$props =[ordered] @{'Name' =$name
'Depends on 1' =$depends[0]
'Depends on 2' =$depends[1]
'Depends on 3' =$depends[2]
}
}else {
$props =[ordered] @{'Name' =$name
'Depends on 1' =$null
'Depends on 2' =$null
'Depends on 3' =$null
}
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
}
$Services | Expand-DependentServices
Name Depends on 1 Depends on 2 Depends on 3
---- ------------ ------------ ------------
BITS
CryptSvc AppIDSvc applockerfltr
I get the following output, for the Write-Output command at the bottom of the code. When I do the export to CSV or conversion/export of the html, it looks good to me. I’m sure there’s a better solution, but this worked for me.
BTW, don’t trust how the Write-Output looks in my above post. It’s just the way this forum formats it. It looks the way it should, on the screen and in the files, when you run the code.
Kevyn, thanks for the help. I hadn’t had time to respond back to you. This worked better than what I was able to come up with. As an FYI, I essentially created an empty CSV file, and then using Add-Content I manually added entries to the CSV file with comma delimiters, using a for loop with index. Its similar, but the code you showed looked cleaner, and lets me remain standardized on export-csv. Thanks!