簡體   English   中英

增強 PowerShell 腳本以在 AsBuiltReport Framework 中查詢 GPO 上的端口

[英]Enhancement of PowerShell script to query ports on GPO within AsBuiltReport Framework

我得到了我想要改進的當前腳本(來自這個答案)。

如果在入站方向內啟用並允許,腳本應該檢索所需的端口。 Action/Enabled/Direction 的過濾器工作得很好,但我仍然需要 Local 端口的過濾器將僅檢索定義端口內的唯一結果,但仍顯示其他端口。

附加問題:

  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}}


`

謝謝

我將 Select-Object -Unique 放在過濾器之前以獲得唯一的結果。

我將 -match 放在范圍端口的相關端口和條件之前。

我希望查詢結果與即將到來的端口端口 80 或 135 或 139 或 445 或 5985 或 5986 或范圍在 49152 和 65535 之間的唯一值

這是我會怎么做。 大約需要 8 秒。 使用 invoke-command,pscomputername 有主機名,還有一個 runspaceid。 -pv 是管道變量。 我必須將 get.netfirewallrule 放在 % 或 foreach-object 中,以便它針對端口信息的每個實例運行。

令人惱火的是 localport 可以是 object 數組或帶破折號的字符串或像“Any”這樣的詞。 用數字過濾它們是有問題的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM