Partager via


RemovePrograms-com.vbs

By The Scripting Guys, Microsoft Corporation

This script deletes exceptions for specified applcations in Windows Firewall. To perform this task, it uses the Windows Firewall COM automation server. It runs only against the local computer. To remove exceptions without removing stored application settings, use ClosePrograms-com.vbs.

RemovePrograms-com.vbs corresponds to CloseProgram.vbs, one of the scripts that ships with Application Compatibility Testing and Mitigation Guide for Windows XP Service Pack 2 and which are documented in the Appendix. You can download a Windows Installer (.msi) file that installs the Guide and its associated scripts from:

https://www.microsoft.com/downloads/details.aspx?FamilyId=9300BECF-2DEE-4772-ADD9-AD0EAF89C4A7&displaylang=en

To use the script, copy the code, paste it into Notepad, and then save the script as RemovePrograms-com.vbs. To run the script, open a command prompt window to the directory of the script and type:

cscript removeprograms-com.vbs

If the default script host on the computer is Cscript.exe, the initial "cscript" may be omitted.

Script Code

'******************************************************************************
'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

For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.

Disclaimer

This sample script is not supported under any Microsoft standard support program or service. The sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.