Running Batch/VBS Scripts when Connected/Disconnected in Viscosity for Windows
Knowledge Base > Viscosity Windows > Running Batch/VBS Scripts when Connected/Disconnected in Viscosity for Windows
Viscosity 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:
- Open Viscosity's Preferences window
- Select the connection you wish to add a script to and click the Edit button
- Click the Advanced tab

- If you'd like a script to run when your VPN connection becomes connected, click the Select button next to the "Connected Script" field. Likewise, if you want your script to run when your connection becomes disconnected, click the Select button next to the "Disconnected Script" field.
Writing Scripts
Writing scripts in Windows is quite simple. Follow the instructions below to create a script.
- Open notepad by going to Start->Programs->Accessories->Notepad
- A new blank window should appear. Enter the the following as a simple script:
MsgBox "Hello There!"

- Go to File->Save.
- 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.

- 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)
|