OpenPrograms-com.vbs

作者: The Scripting Guys,Microsoft Corporation

這個指令碼會將指定的程式新增到 Windows 防火牆例外清單。指令碼會使用 Windows 防火牆 COM 自動化伺服器執行這項工作。它只在本機電腦上執行。

OpenPrograms-com.vbs 對應於 OpenProgram.vbs;OpenProgram.vbs 是《Application Compatibility Testing and Mitigation Guide for Windows XP Service Pack 2》(Windows XP Service Pack 2 應用程式相容性測試及緩和指南) 隨附的指令碼之一,並記錄在<附錄>中。您可以下載用來安裝該指南及其相關指令碼的 Windows Installer (.msi) 檔案,網址是:

http://www.microsoft.com/downloads/details.aspx?FamilyId=9300BECF-2DEE-4772-ADD9-AD0EAF89C4A7&displaylang=en (英文)

若要使用指令碼,請複製程式碼並將它貼入「記事本」,再將指令碼儲存為 OpenPrograms-com.vbs。若要執行指令碼,請將命令提示視窗開啟到指令碼的目錄,並輸入:

cscript openprograms-com.vbs

如果電腦上的預設指令碼裝載是 Cscript.exe,就可以省略開頭的 cscript。

指令碼


'******************************************************************************
'OpenPrograms-com.vbs
'Author: Peter Costantini, The Microsoft Scripting Guys
'Date: 8/26/04
'Version: 1.0
'This script adds specified programs to the Windows Firewall exceptions list.
'******************************************************************************

Const NET_FW_SCOPE_ALL = 0
Const NET_FW_SCOPE_LOCAL_SUBNET = 1
'First dimension of arrNewApps must equal # of apps to be allowed minus 1.
Dim arrNewApps(2,3)

'Edit this list to add programs to the exceptions list.
'Scope and Enabled are optional properties.
'Default for Scope is NET_FW_SCOPE_ALL. Default for Enabled is True.

arrNewApps(0,0) = "NsLookup" 'Name
arrNewApps(0,1) = "%windir%\system32\nslookup.exe" 'ProcessImageFileName
'Must be a fully qualified path, but can contain environment variables.
arrNewApps(0,2) = NET_FW_SCOPE_ALL 'Scope - default is NET_FW_SCOPE_ALL
arrNewApps(0,3) = True 'Enabled

arrNewApps(1,0) = "Notepad"
arrNewApps(1,1) = "%windir%\system32\notepad.exe"
arrNewApps(1,2) = NET_FW_SCOPE_LOCAL_SUBNET
arrNewApps(1,3) = True

arrNewApps(2,0) = "Calculator"
arrNewApps(2,1) = "%windir%\system32\calc.exe"
arrNewApps(2,2) = NET_FW_SCOPE_ALL
arrNewApps(2,3) = True

On Error Resume Next
'Create the firewall manager object.
Set objFwMgr = CreateObject("HNetCfg.FwMgr")
If Err <> 0 Then
  WScript.Echo "Unable to connect to Windows Firewall."
  WScript.Quit
End If
'Get the current profile for the local firewall policy.
Set objProfile = objFwMgr.LocalPolicy.CurrentProfile
Set colAuthorizedApps = objProfile.AuthorizedApplications

WScript.Echo VbCrLf & "New authorized applications added:"
For i = 0 To UBound(arrNewApps)
'Create an FwAuthorizedApplication object
  Set objAuthorizedApp = CreateObject("HNetCfg.FwAuthorizedApplication")
  objAuthorizedApp.Name = arrNewApps(i,0)
  objAuthorizedApp.ProcessImageFileName = arrNewApps(i, 1)
  objAuthorizedApp.Scope = arrNewApps(i, 2)
  objAuthorizedApp.Enabled = arrNewApps(i, 3)
  colAuthorizedApps.Add objAuthorizedApp
  If Err = 0 Then
    WScript.Echo VbCrLf & "Name: " & objAuthorizedApp.Name
    WScript.Echo "  Process Image File: " & _
     objAuthorizedApp.ProcessImageFileName
    WScript.Echo "  Scope: " & objAuthorizedApp.Scope
    WScript.Echo "  Enabled: " & objAuthorizedApp.Enabled
  Else
    WScript.Echo VbCrLf & "Unable to add application: " & arrNewApps(i,0)
    WScript.Echo "  Error Number:" & Err.Number
    WScript.Echo "  Source:" & Err.Source
    WScript.Echo "  Description:" & Err.Description
  End If
  Err.Clear
Next

Set colAuthorizedApps = objProfile.AuthorizedApplications
WScript.Echo VbCrLf & "All listed applications after operation:"
For Each objApp In colAuthorizedApps
  WScript.Echo VbCrLf & "Name: " & objApp.Name
  WScript.Echo "  Process Image File: " & objApp.ProcessImageFileName
  WScript.Echo "  Scope: " & objApp.Scope
  WScript.Echo "  Remote Address: " & objApp.RemoteAddress
  WScript.Echo "  Enabled: " & objApp.Enabled
Next


顯示: