Click here to Skip to main content
Click here to Skip to main content

VBScript to check - Am I connected to VPN?

By , 9 May 2013
 

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

About the Author

RaisKazi
Technical Lead
United States United States
Member
Born on Earth.
 
"Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
 
Cheers !!! Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3 Pinmemberemartinho9 May '13 - 6:09 
GeneralRe: My vote of 3 PinmemberRaisKazi9 May '13 - 8:04 

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 9 May 2013
Article Copyright 2013 by RaisKazi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid