Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VBScript to check - Am I connected to VPN?

0.00/5 (No votes)
9 May 2013 1  
VBScript to check - Am I connected to VPN?

Introduction

Recently while developing automation utility using VBScript, I had to check if my machine is connected to VPN. I gone through many links on Google to find exact code of my requirement and get my automation done. But couldn't found the exact code which checks, if machine is connected to VPN. Sharing VBScript code here which tells you if your machine is connected to VPN.

Using the code

We are going to create below two files to test this code.

  1. testScript.vbs (VBScript file)
  2. testBatch.bat (batch file to call the above VBScript file)

Create testScript.vbs (VBScript file) and add below code in it.

Wscript.Echo IsVPNConnected()

Function IsVPNConnected()
   
   IsVPNConnected = False
   sComputer = "." 

   Set oWMIService = GetObject("winmgmts:\\" _
    & sComputer & "\root\CIMV2") 

    
   Set colItems = oWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration",,48) 

    
   For Each objItem in colItems 

'Please check description of your VPN Connection by running command "ipconfig /all" on command-line.

    If(InStr(LCase(objItem.Description),"vpn")) Then
     IsVPNConnected = objItem.IPEnabled     
    End If

   Next
   

   If(IsVPNConnected) Then
    IsVPNConnected =  "I am Connected to VPN."
   Else
    IsVPNConnected = "I am Not Connected to VPN."
   End If

End Function

Create testBatch.bat file using Notepad and add below lines to it.

cscript /nologo testScript.vbs

pause

Run testBatch.bat file to see if you are connected to VPN.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here