Windows Firewall をコマンド制御する (netshコマンド)

20 12月, 2013 (17:14) | あやめ, パソコン | By: ohishi

Web参照やメールの送受信を停止する必要が出たため、コマンドでFirewallの設定変更するVBSを書いたので記載しておきます

◆処理内容

・ポート80,110,537を停止するプロファイルを登録する
・登録したプロファイルを削除する

◆テスト環境

・Windows7,Windows8.1

◆VBSソース

'============================================================================
' webstop : Windows firewall add              create t.ohishi 2013/12/20
'============================================================================
Option Explicit
On Error Resume Next

Dim objWshShell
Dim str1,str2,str3
str1 = "netsh advfirewall firewall add rule name=""Close Port 80"" dir=out action=block protocol=tcp remoteport=80 profile=private,public"
str2 = "netsh advfirewall firewall add rule name=""Close Port 110"" dir=out action=block protocol=tcp remoteport=110 profile=private,public"
str3 = "netsh advfirewall firewall add rule name=""Close Port 537"" dir=out action=block protocol=tcp remoteport=537 profile=private,public"

'管理者モードで実行
Dim WMI, OS, Value, Shell
do while WScript.Arguments.Count = 0 and WScript.Version >= 5.7
    Set WMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set OS = WMI.ExecQuery("SELECT *FROM Win32_OperatingSystem")
    For Each Value in OS
     if left(Value.Version, 3) < 6.0 then exit do
    Next
    Set Shell = CreateObject("Shell.Application")
    Shell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ uac", "", "runas"
    WScript.Quit
loop

'コマンド実行
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Run str1,0,false
objWshShell.Run str2,0,false
objWshShell.Run str3,0,false
Set objWshShell = Nothing
'============================================================================
' webstart : Windows firewall delete              create t.ohishi 2013/12/20
'============================================================================
Option Explicit
On Error Resume Next

Dim objWshShell
Dim str1,str2,str3
str1 = "netsh advfirewall firewall delete rule name=""Close Port 80"" protocol=tcp remoteport=80 profile=private,public"
str2 = "netsh advfirewall firewall delete rule name=""Close Port 110"" protocol=tcp remoteport=110 profile=private,public"
str3 = "netsh advfirewall firewall delete rule name=""Close Port 537"" protocol=tcp remoteport=537 profile=private,public"

'管理者モードで実行
Dim WMI, OS, Value, Shell
do while WScript.Arguments.Count = 0 and WScript.Version >= 5.7
    Set WMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set OS = WMI.ExecQuery("SELECT *FROM Win32_OperatingSystem")
    For Each Value in OS
     if left(Value.Version, 3) < 6.0 then exit do
    Next
    Set Shell = CreateObject("Shell.Application")
    Shell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ uac", "", "runas"
    WScript.Quit
loop

'コマンド実行
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Run str1,0,false
objWshShell.Run str2,0,false
objWshShell.Run str3,0,false
Set objWshShell = Nothing

Write a comment