App Support.

We're here to help.



Running Batch/VBS Scripts when Connected/Disconnected

Viscosity Windows allows you to run custom Batch (.bat) or Visual Basic/VBS (.vbs) scripts when your connection connects or disconnects. This can allow you to automate common tasks, such as connecting to file servers, opening web pages, opening applications, and controlling any application that is scriptable in Batch or VBS.

Selecting Scripts To Use

Viscosity makes it easy to specify scripts to run when a connection connects or disconnects, and allows you to specify different scripts for each connection. You can specify a script like so:

  1. Open Viscosity's Preferences window
  2. Select the connection you wish to add a script to and click the Edit button
  3. Click the Advanced tab


  4. Click the Select button next to time you would like the script to run, browse to the script on your hard drive then click Open.
  5. Click Save.

Viscosity offers three times to run a script during the connection cycle. Before Connect will run a script right when you click to connect a connection. Connected will run a script when the connection is connected and active, and Disconnected will run a script when the connection has completed it's disconnect procedures.

Writing Scripts

Writing scripts in Windows is quite simple. Follow the instructions below to create a script.

  1. Open notepad by going to Start->Programs->Accessories->Notepad
  2. A new blank window should appear. Enter the the following as a simple script:

    MsgBox "Hello There!"


  3. Go to File->Save.
  4. Change the "Save as type:" to All Files, and enter a name for your file followed by .vbs. If you are creating a Batch script, append .bat instead.


  5. Test the script by double clicking it where you saved it.

Common Tasks

The following are example VBS or Batch script snippets for common tasks. These can be combined to create your Connected/Disconnected scripts.

Mount a Network Drive

The following Batch script can be used to mount a network drive to a remote file server. 'Z:' can be changed to any drive letter you require. Place the following code in your Connected script.

@echo off
net use Z: \\server\share

Place the following code in your Disconnected script to disconnect the network drive when the VPN connection is terminated.

@echo off
net use Z: /delete

Display A Message To The User

The following VBS script can be used to display a message to the user when they connect. It could be used to display a welcome message, instructions or even Terms of Use for your service. Place the following code in your Connected Script.

MsgBox "Hello There!"

Open A Webpage

The following VBS script will automattically open the user's default web browser and go to the specified web page. This could be used to automatically open a company's Intranet webpage, display a welcome webpage, etc.

URL = "http://www.thesparklabs.com"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(URL)

Close A Program

The following VBS script will automatically close/kill a running process when run. This is handy for when you want a close a program when your VPN disconnects for example. Simply replace 'notepad.exe' below with the name of the executable you want to kill. There is a caveat to this, Viscosity must be running with the same privileges or higher than the user that started the application.

Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'notepad.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
' End of WMI Example of a Kill Process