Windows Firewall のルールを変更する場合、コントロールパネル(GUI)から設定するだけでなく、PowerShell(CLI)から実施することも可能です。 以下は ICMPv4、TCP、UDP 各々のプロトコルで Windows Firewall を新規設定(追加)してみます。
ICMPv4 を許可する
New-NetFirewallRule -Name Permit_ICMPv4 -DisplayName “Permit ICMPv4” -Description “Permit ICMPv4” -Protocol ICMPv4 -IcmpType 8 -Enabled True -Profile Any -Action Allow
TCP/10,000 を許可する
New-NetFirewallRule -Name Permit_TCP10000 -DisplayName "Permit TCP/10000" -Description "Permit TCP/10000" -Protocol TCP -RemotePort 10000 -Enabled True -Profile Any -Action Allow
UDP/10,000 を許可する
New-NetFirewallRule -Name Permit_UDP10000 -DisplayName "Permit UDP/10000" -Description "Permit UDP/10000" -Protocol UDP -RemotePort 10000 -Enabled True -Profile Any -Action Allow
管理者権限が無く、設定にした場合の表示
PowerShell プロンプトに管理者権限が無く、Firewall ルールの設定に失敗した場合は以下のような表示になります。
PS C:\Users\user> New-NetFirewallRule -Name Permit_UDP10000 -DisplayName "Permit UDP/10000" -Description "Permit UDP/100 00" -Protocol UDP -RemotePort 10000 -Enabled True -Profile Any -Action Allow New-NetFirewallRule : アクセスが拒否されました。 発生場所 行:1 文字:1 + New-NetFirewallRule -Name Permit_UCP10000 -DisplayName "Permit UDP/10 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (MSFT_NetFirewallRule:root/standardcimv2/MSFT_NetFirewallRule) [New-Ne tFirewallRule], CimException + FullyQualifiedErrorId : Windows System Error 5,New-NetFirewallRule
設定に成功した場合の表示
PowerShell プロンプトに管理者権限があり、Firewall ルールの設定に成功した場合は以下のような表示になります。
PS C:\Windows\system32> New-NetFirewallRule -Name Permit_UDP10000 -DisplayName "Permit UDP/10000" -Description "Permit U DP/10000" -Protocol UDP -RemotePort 10000 -Enabled True -Profile Any -Action Allow Name : Permit_UCP10000 DisplayName : Permit UDP/10000 Description : Permit UDP/10000 DisplayGroup : Group : Enabled : True Profile : Any Platform : {} Direction : Inbound Action : Allow EdgeTraversalPolicy : Block LooseSourceMapping : False LocalOnlyMapping : False Owner : PrimaryStatus : OK Status : 規則は、ストアから正常に解析されました。 (65536) EnforcementStatus : NotApplicable PolicyStoreSource : PersistentStore PolicyStoreSourceType : Local