I got the current script (from this answer) which I want to improve.
The script should retrieve the desired ports if they are enabled and allowed within the inbound direction. Filters for Action/Enabled/Direction work perfectly but I still need that the filters for the Local ports will retrieve only unique results within the defined ports but still show other ports as well.
Additional question:
- how do I add the IP of the machine to the query?
- I would like to use AsBuiltReport to publish the results. How does it do?
- I would like to trigger it remotely I do it through invoke command but if there is a best practice for this I would like to be aware of that.
- How can I have only the relevant ports I mentioned and not anything else?
`
Get-NetFirewallRule -Action Allow -Enabled True -Direction Inbound |
Where-Object {
$portFilter = $PSItem | Get-NetFirewallPortFilter | Select-Object -Unique
$portFilter.LocalPort -match '^(80|135|139|445|5985|5986)$' -or
($portFilter.LocalPort -ge 49152 -and $portFilter.LocalPort -le 65535)} |
Format-Table Name,Profile,
Enabled,
Direction,
Action,
@{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}},
@{ Name='LocalPort'; Expression={$portFilter.LocalPort | Select-Object -Unique}},
@{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}}
`
Thanks
I put the Select-Object -Unique before the filters to get only unique results.
I put -match before the relevant ports and conditions for the range port.
I expect the query to result unique values with the coming ports Ports 80 or 135 or 139 or 445 or 5985 or 5986 or range between 49152 and 65535