Search code examples
vbscriptwminetwork-protocols

VBS Script to Check Port Availability in Windows


I am trying to check preconditions for a certain piece of software. I have a set of scripts that goes through and checks for things such as disk space, memory availability, etc.

I need to create a script that can check if certain ports are open and accessible. I am using the WMI to check other network configuration items, but cannot find any references to checking port availability.

Anyone have any ideas where I can find a WMI construct to let me view and manage ports or any other ideas for how to structure a new script to check port availability?


Solution

  • So I found a script someone had made a while back. It uses netStat. Should work on just about any windows machine. I think I was blinded by my desire to use the WMI.

    Sub PortMonitor (strCommand2)
    
      Set StdOut = WScript.StdOut
      Set objShell = CreateObject("WScript.Shell")
      set objScriptExec = objShell.Exec (strCommand2)
    
      strPingResults = LCase(objScriptExec.StdOut.ReadAll)
    
      if len (strPingResults) > 0 then
         WScript.Echo "Established!"
      End if
    end Sub
    
    Dim strcommand
    strCommand = "cmd /C ""netStat -n |find ""127.0.0.1:1137"" | find ""ESTABLISHED"""""
    Call PortMonitor (strCommand)
    

    OP: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/2003_Server/Q_27476065.html#