增强 PowerShell 脚本以查询 AsBuiltReport 框架内 GPO 上的端口

2023-12-12

我得到了当前的脚本(来自这个答案)我想改进。

如果在入站方向内启用并允许这些端口,则脚本应检索所需的端口。操作/启用/方向的过滤器工作完美,但我仍然需要本地端口的过滤器仅检索定义端口内的唯一结果,但仍显示其他端口。

附加问题:

  1. 如何将机器的 IP 添加到查询中?
  2. 我想使用 AsBuiltReport 来发布结果。效果如何?
  3. 我想通过调用命令远程触发它,但如果有最佳实践,我想知道这一点。
  4. 我怎样才能只拥有我提到的相关端口而没有其他任何端口?

`

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

我将 Select-Object -Unique 放在过滤器之前,以仅获得唯一的结果。

我将 -match 放在相关端口和范围端口条件之前。

我希望查询会产生与即将到来的端口端口 80 或 135 或 139 或 445 或 5985 或 5986 或范围在 49152 和 65535 之间的唯一值


我就是这样做的。大约需要8秒。对于invoke-command,pscomputername 具有主机名,并且还有一个runspaceid。 -pv 是管道变量。我必须将 get-netfirewallrule 放入 % 或 foreach-object 中,以便它针对端口信息的每个实例运行。

令人烦恼的是,本地端口可以是对象数组或带有破折号的字符串或“任何”之类的单词。对它们进行数字过滤是有问题的。

LocalPort                    type
---------                    ----
546                          System.String
{554, 8554-8558}             System.Object[]
5000-5020                    System.String
{554, 8554, 8555, 8556...}   System.Object[]
{80, 443}                    System.Object[]
Any                          System.String
IPHTTPSIn                    System.String
PlayToDiscovery              System.String
RPC                          System.String
RPCEPMap                     System.String
Teredo                       System.String

1024-65535
5000-5020
8554-8558
7200-17210
invoke-command localhost { Get-NetFirewallPortFilter | 
  ? {
  80 -in $_.localport -or
  135 -in $_.localport -or
  139 -in $_.localport -or
  445 -in $_.localport -or
  5985 -in $_.localport -or
  5986 -in $_.localport -or
  $(if($_.localport -as 'int') { ([int]$_.LocalPort -ge 49152 -and
    [int]$_.LocalPort -le 65535) } )
  } -pv port | 
  % { $_ | Get-NetFirewallRule } |
  ? { $_.action -eq 'allow' -and $_.enabled -eq $true -and
  $_.direction -eq 'inbound' } | 
  select Name,Profile,Enabled,Direction,Action,
  @{n='Protocol';e={$port.Protocol}},
  @{n='Localport';e={$port.Localport}},
  @{n='Remoteport';e={$port.Remoteport}} } | ft -a


Name                          Profile         Enabled Direction Action Protocol Localport Remoteport PSComputerName RunspaceId
----                          -------         ------- --------- ------ -------- --------- ---------- -------------- ----------
WINRM-HTTP-In-TCP-NoScope     Domain, Private True    Inbound   Allow  TCP      5985      Any        localhost      8366c72c-e868-4e50-adb5-85c4deea3583
WINRM-HTTP-In-TCP             Public          True    Inbound   Allow  TCP      5985      Any        localhost      8366c72c-e868-4e50-adb5-85c4deea3583
IIS-WebServerRole-HTTP-In-TCP Any             True    Inbound   Allow  TCP      80        Any        localhost      8366c72c-e868-4e50-adb5-85c4deea3583
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

增强 PowerShell 脚本以查询 AsBuiltReport 框架内 GPO 上的端口 的相关文章

随机推荐