RemovePrograms-com.vbs

作者: The Scripting Guys,Microsoft Corporation

這個指令碼會刪除 Windows 防火牆的指定應用程式例外。指令碼會使用 Windows 防火牆 COM 自動化伺服器執行這項工作。它只在本機電腦上執行。若要移除例外而不移除儲存的應用程式設定,請使用 ClosePrograms-com.vbs。

RemovePrograms-com.vbs 對應於 CloseProgram.vbs;CloseProgram.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 (英文)

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

cscript removeprograms-com.vbs

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

指令碼


'******************************************************************************
'RemovePrograms-com.vbs
'Author: Peter Costantini, The Microsoft Scripting Guys
'Date: 8/18/04
'Version: 1.0
'This script deletes exceptions for specified apps in Windows Firewall.
'To remove exceptions without removing stored application settings,
'use ClosePrograms-com.vbs.
'******************************************************************************

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

'Edit this list to remove programs from the exceptions list.
arrRemoveApps(0,0) = "NsLookup" 'Name
arrRemoveApps(0,1) = "c:\windows\system32\nslookup.exe" 'ProcessImageFileName
'Must be a fully qualified path. Cannot contain environment variables.

arrRemoveApps(1,0) = "Notepad"
arrRemoveApps(1,1) = "c:\windows\system32\notepad.exe"

arrRemoveApps(2,0) = "Calculator"
arrRemoveApps(2,1) = "c:\windows\system32\calc.exe"

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 & "Authorized applications removed:"
For i = 0 To UBound(arrRemoveApps)
  intCount = 0
  For Each objAuthorizedApp In colAuthorizedApps
    If LCase(objAuthorizedApp.ProcessImageFileName) = arrRemoveApps(i, 1) Then
      intCount = 1
      strName = objAuthorizedApp.Name
      strProcessImageFileName = objAuthorizedApp.ProcessImageFileName
      intScope = objAuthorizedApp.Scope
      blnEnabled = objAuthorizedApp.Enabled
      colAuthorizedApps.Remove objAuthorizedApp.ProcessImageFileName
      Exit For
    End If
  Next
  If intCount = 1 Then
    If Err = 0 Then
      WScript.Echo VbCrLf & "Name: " & strName
      WScript.Echo "  Process Image File: " & strProcessImageFileName
      WScript.Echo "  Scope: " & intScope
      WScript.Echo "  Enabled: " & blnEnabled
    Else
      WScript.Echo VbCrLf & "Unable to remove application: " & _
       arrRemoveApps(i,0)
      WScript.Echo "  Error Number:" & Err.Number
      WScript.Echo "  Source:" & Err.Source
      WScript.Echo "  Description:" & Err.Description
    End If
    Err.Clear
  Else
    WScript.Echo VbCrLf & "Application " & arrRemoveApps(i, 0) & _
     " not found."
  End If
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 "  Enabled: " & objApp.Enabled
Next


如需線上對等支援,請加入 msnews.microsoft.com 新聞伺服器上的 microsoft.public.windows.server.scripting (英文) 社群。若您想要對範例指令碼或指令碼指南,提供意見、回報問題,請與 Microsoft TechNet (英文) 連絡。

免責聲明

此範例指令碼不支援任何 Microsoft 標準技術支援方案或服務。上述的範例指令碼係依「現況」提供,不附帶任何擔保。Microsoft 公司不提供任何的默示擔保,包括但不限於任何商業適售性及特定用途之適用性的默示擔保。您必須承擔此範例指令碼或文件所造成的一切風險。在任何情況下,無論是使用或無法使用此範例指令碼或文件所造成的損害 (包括但不限於營業之損失、營業之中斷、營業資訊之滅失及其他金錢損失),Microsoft 公司、作者群或此指令碼之創作、製造或散發有關之人員概不負責,即使 Microsoft 已經被告知損害發生之可能性亦同。

顯示: