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.
IsVPNConnected = False
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" _
& sComputer & "\root\CIMV2")
Set colItems = oWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
IsVPNConnected = objItem.IPEnabled
IsVPNConnected = "I am Connected to VPN."
IsVPNConnected = "I am Not Connected to VPN."
Create testBatch.bat file using Notepad and add below lines to it.
cscript /nologo testScript.vbs
Run testBatch.bat file to see if you are connected to VPN.