Windowsファイアーウォールをバッチで操作するいろいろ(Powershell/netsh(DOS)/VBScript)
スポンサーリンク
ローカル環境の複数のWindowsサーバのファイアーウォールを設定する要件があり、いろいろとリモート実行できないかと調べたのですがどうもできないようで。
妥協案ですがファイルサーバにバッチを置いて、ジョブサーバから該当サーバのエージェント経由でファイルサーバ上のファイアーウォール設定・追加を一括実行するということにしました。
その時、各言語でファイアーウォールを操作する基本コマンドを調べたので記す。
目次
各実装方法のサンプル
それぞれのスクリプトサンプルを記載してます。
Powershell
設定追加
表示名 test で新規設定(xxxx.ps1)
$newDefinition = "test"
$Name=$newDefinition
$DisplayName=$newDefinition
$Description="Windows PowerShell Remoting required to open for public connection. not for private network."
$Group="Windows Remote Management"
$Enabled="True"
$Profile="Any"
$Direction="Outbound"
$Action="Block"
$Program="Any"
$LocalAddress="Any"
$RemoteAddress="192.168.5.1-192.168.5.10"
$Protocol="TCP"
$LocalPort="Any"
$RemotePort="1000-2000"
$LocalUser="Any"
$RemoteUser="Any"
if(-not(Get-NetFirewallRule | where Name -eq $newDefinition)){
New-NetFirewallRule `
-Name $newDefinition `
-DisplayName $newDefinition `
-Description $newDefinition `
-Group $Group `
-Enabled $Enabled `
-Profile $Profile `
-Direction $Direction `
-Action $Action `
-EdgeTraversalPolicy Block `
-LooseSourceMapping $False `
-LocalOnlyMapping $False `
-OverrideBlockRules $False `
-Program $Program `
-LocalAddress $LocalAddress `
-RemoteAddress $RemoteAddress `
-Protocol $Protocol `
-LocalPort $LocalPort `
-RemotePort $RemotePort `
-LocalUser $LocalUser `
-RemoteUser $RemoteUser
}else{
Write-Host "///////////////////////////////////////////////////////////////"
Write-Host $newDefinition " is arleady exist. the definition is bellow"
Write-Host "///////////////////////////////////////////////////////////////"
Show-NetFirewallRule | where DisplayName -eq $newDefinition
}
無効化
表示名 test を無効化
PS >Disable-NetFirewallRule -DisplayName "test"
有効化
表示名 test を有効化
PS >Enable-NetFirewallRule -DisplayName "test"
一覧表示
現在の設定詳細一覧表示
PS > Show-NetFirewallRule
Name : {BD9C612E-69AA-434C-91CD-A33D28C115C4}
DisplayName : test
Description : dddddddesctiprinn
DisplayGroup :
Group :
Enabled : True
Profile : Any
Platform :
Direction : Outbound
Action : Block
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
Owner :
PrimaryStatus : OK
Status : The rule was parsed successfully from the store. (65536)
EnforcementStatus : NotApplicable
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
$_ | Get-NetFirewallAddressFilter
LocalAddress : Any
RemoteAddress : 192.168.5.1-192.168.5.10
$_ | Get-NetFirewallServiceFilter
Service : Any
$_ | Get-NetFirewallApplicationFilter
Program : Any
Package :
$_ | Get-NetFirewallInterfaceFilter
InterfaceAlias : Any
$_ | Get-NetFirewallInterfaceTypeFilter
InterfaceType : Any
$_ | Get-NetFirewallPortFilter
Protocol : TCP
LocalPort : Any
RemotePort : 1000 2000
IcmpType : Any
DynamicTarget : Any
$_ | Get-NetFirewallSecurityFilter
Authentication : NotRequired
Encryption : NotRequired
OverrideBlockRules : False
LocalUser : Any
RemoteUser : Any
RemoteMachine : Anyこれをリモートコンピュータに設定するには事前に「Enable-PSRemoting」をやっておかないといけない。
PowerShellでリモートPCの操作を行うに為にEnable-PSRemotingをするための準備 - tech.guitarrapc.cóm
Netsh
設定追加
「block80」というルール名前でTCPの80番ポート送信をブロックする。
netsh advfirewall firewall add rule name="block80" protocol=TCP dir=out remoteport=80 action=block remoteip=any
無効化
netsh advfirewall firewall set rule name="block80" new enable=no
有効化
netsh advfirewall firewall set rule name="block80" new enable=yes
表示
C:\Users\miyashita-a>netsh advfirewall firewall show rule name="allow80" verbose 規則名: block80 ---------------------------------------------------------------------- 有効: いいえ 方向: 出力 プロファイル: ドメイン,プライベート,パブリック グループ: ローカル IP: 任意 リモート IP: 任意 プロトコル: TCP ローカル ポート: 任意 リモート ポート: 80 エッジ トラバーサル: いいえ 操作: ブロック OK
削除
netsh advfirewall firewall delete rule name="block80"
参考(マニュアル)
ファイアーウォール設定画面のショートカット
firewall.cpl

ネットワーク管理に使うWMI―PowerShellによるWindowsマシン管理
- 作者: 嶋貫健司
- 出版社/メーカー: カットシステム
- 発売日: 2016/08
- メディア: 単行本
- この商品を含むブログ (1件) を見る