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.
- testScript.vbs (VBScript file)
- 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
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.