LoginSignup
1
2

More than 1 year has passed since last update.

WindowsFirewallの規則をIPアドレス、ポート番号情報込みで一括取得するPowerShellスクリプト

Posted at

WindowsFirewallの受信/送信の規則をGUIで確認するようなイメージでCSV出力するPowerShellスクリプトの紹介です。
ネットで見つけたスクリプトを参考に少しだけアレンジしたものになります。
CSV出力ファイル名に実行したマシンのホスト名と日時が含まれるようにしていますので、
サポートなどで他の人のWindowsFirewallルールを確認したい場合などにスクリプトを送付して情報取得依頼をする場合に利用できるかと思います。

Windows_Firewall_rule_export_csv2 .ps1
Import-Module NetSecurity

$Hostname = $Env:Computername

$Datetime = Get-Date -Format "yyyyMMdd-HHmm"

$Filename = $Hostname + "_" + $Datetime

Get-NetFirewallRule | Where-Object {$_.enabled -eq "true" -and $_.direction -eq "inbound"} |
ForEach-Object{
$Rule = $_
$Port = $_ | Get-NetFirewallPortFilter
$Addr = $_ | Get-NetFirewallAddressFilter
$App = $_ | Get-NetFirewallApplicationFilter
$Sec = $_ | Get-NetFirewallSecurityFilter
[pscustomobject]@{
Name = $Rule.DisplayName
Group = $Rule.Group
Profile = $Rule.Profile
Enabled = $Rule.Enabled
Action = $Rule.Action
Override = $Sec.OverrideBlockRules
Program = $App.Program
Protocol = $Port.Protocol
LocalAddress = $Addr.LocalAddress
LocalPort = $Port.LocalPort
RemoteAddress = $Addr.RemoteAddress
RemotePort = $Port.RemotePort
}

} | export-csv ./WindowsFirewall_Inbound_Rule_$Filename.txt -encoding default

Get-NetFirewallRule |  Where-Object {$_.enabled -eq "true" -and $_.direction -eq "outbound"} |
ForEach-Object{
$Rule = $_
$Port = $_ | Get-NetFirewallPortFilter
$Addr = $_ | Get-NetFirewallAddressFilter
$App = $_ | Get-NetFirewallApplicationFilter
$Sec = $_ | Get-NetFirewallSecurityFilter
[pscustomobject]@{
Name = $Rule.DisplayName
Group = $Rule.Group
Profile = $Rule.Profile
Enabled = $Rule.Enabled
Action = $Rule.Action
Override = $Sec.OverrideBlockRules
Program = $App.Program
Protocol = $Port.Protocol
LocalAddress = $Addr.LocalAddress
LocalPort = $Port.LocalPort
RemoteAddress = $Addr.RemoteAddress
RemotePort = $Port.RemotePort
}
       
}   |  export-csv ./WindowsFirewall_Outbound_Rule_$Filename.txt -encoding default

Write-Output "Success"

実行方法
コマンドプロンプトにて

powershell

.\Windows_Firewall_rule_export_csv2.ps1

処理が終了すると画面に「Success」と表示されます。

スクリプトを配置したフォルダと同じ場所に
下記2ファイルが出力されます。

WindowsFirewall_Inbound_Rule_[ホスト名]_[yyyymmdd-HHmm].txt
WindowsFirewall_Outbound_Rule__[ホスト名]_[yyyymmdd-HHmm].txt

ネットでイメージ通りの出力をしてくれる書き方がなかなか見つける事が出来なかったので備忘録として残しておきます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2