Share via


ClosePorts-com.vbs

By The Scripting Guys, Microsoft Corporation

This script closes specified ports in Windows Firewall by setting the Enabled property to False, while retaining the stored port settings. To perform this task, it uses the Windows Firewall COM automation server. It runs only against the local computer.

ClosePorts-com.vbs does not correspond exactly to ClosePort.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. Where ClosePort.vbs deletes an exception for only one port, ClosePorts-com.vbs disables but saves exceptions for multiple ports. 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 ClosePorts-com.vbs. To run the script, open a command prompt window to the directory of the script and type:

cscript closeports-com.vbs

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

Script Code

'******************************************************************************
'ClosePorts-com.vbs
'Author: Peter Costantini, The Microsoft Scripting Guys
'Date: 8/30/04
'Version: 1.0
'This script closes specified ports in Windows Firewall by setting the 
'Enabled property to False. It retains the stored port settings.
'******************************************************************************

Const NET_FW_IP_PROTOCOL_TCP = 6
Const NET_FW_IP_PROTOCOL_UDP = 17
'First dimension of arrClosePorts  must equal # of ports minus 1.
Dim arrClosePorts(2,1)

'Edit this list to list ports to close (disable).
arrClosePorts(0,0) = 137 'Port
arrClosePorts(0,1) = NET_FW_IP_PROTOCOL_TCP 'Protocol

arrClosePorts(1,0) = 138
arrClosePorts(1,1) = NET_FW_IP_PROTOCOL_TCP

arrClosePorts(2,0) = 552
arrClosePorts(2,1) = NET_FW_IP_PROTOCOL_UDP

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 colOpenPorts = objProfile.GloballyOpenPorts

WScript.Echo VbCrLf & "Ports closed (disabled):"
For i = 0 To UBound(arrClosePorts)
  intCount = 0
  For Each objOpenPort In colOpenPorts
    If (objOpenPort.Port = arrClosePorts(i, 0)) And _
     (objOpenPort.Protocol = arrClosePorts(i, 1)) Then
      intCount = 1
      objOpenPort.Enabled = False
      strName = objOpenPort.Name
      intPort = objOpenPort.Port
      intProtocol = objOpenPort.Protocol
      intScope = objOpenPort.Scope
      Exit For
    End If
  Next
  If intCount = 1 Then
    If Err = 0 Then
      WScript.Echo VbCrLf & "Name: " & strName
      WScript.Echo "  Protocol: " & intProtocol
      WScript.Echo "  Port Number: " & intPort
      WScript.Echo "  Scope: " & intScope
    Else
      WScript.Echo VbCrLf & "Unable to close port: " & intProtocol & _
       " " & intNumber
      WScript.Echo "  Error Number:" & Err.Number
      WScript.Echo "  Source:" & Err.Source
      WScript.Echo "  Description:" & Err.Description
    End If
    Err.Clear
  Else
    WScript.Echo VbCrLf & "Port " & arrClosePorts(i, 1) & _
     " " & arrClosePorts(i, 0) & " not found."
  End If
Next

Set colOpenPorts = objProfile.GloballyOpenPorts
WScript.Echo VbCrLf & "All listed ports after operation:"
For Each objPort In colOpenPorts
  WScript.Echo VbCrLf & "Name: " & objPort.Name
  WScript.Echo "  Protocol: " & objPort.Protocol
  WScript.Echo "  Port Number: " & objPort.Port
  WScript.Echo "  Scope: " & objPort.Scope
  WScript.Echo "  Enabled: " & objPort.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.