Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / VBScript
Tip/Trick

VBScript to check - Am I connected to VPN?

Rate me:
Please Sign up or sign in to vote.
3.86/5 (3 votes)
9 May 2013CPOL 43.9K   2   10
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.

VBScript
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDidn't work for me Pin
twisterat574-Apr-17 4:31
professionaltwisterat574-Apr-17 4:31 
QuestionThank you. Pin
ryad ramoul31-Oct-13 3:48
ryad ramoul31-Oct-13 3:48 
AnswerRe: Thank you. Pin
RaisKazi31-Oct-13 3:51
RaisKazi31-Oct-13 3:51 
Check description of your VPN Connection by running command "ipconfig /all" on command-line. Then change the If(InStr(LCase(objItem.Description),"vpn")) condition based on description of your VPN.
Change is a pattern of life.

GeneralRe: Thank you. Pin
ryad ramoul31-Oct-13 4:09
ryad ramoul31-Oct-13 4:09 
GeneralRe: Thank you. Pin
RaisKazi31-Oct-13 5:59
RaisKazi31-Oct-13 5:59 
GeneralRe: Thank you. Pin
Tuân Hồ15-Jul-14 6:53
Tuân Hồ15-Jul-14 6:53 
GeneralRe: Thank you. Pin
RaisKazi15-Jul-14 8:20
RaisKazi15-Jul-14 8:20 
GeneralRe: Thank you. Pin
Tuân Hồ16-Jul-14 8:39
Tuân Hồ16-Jul-14 8:39 
GeneralMy vote of 3 Pin
emartinho9-May-13 6:09
emartinho9-May-13 6:09 
GeneralRe: My vote of 3 Pin
RaisKazi9-May-13 8:04
RaisKazi9-May-13 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.