Virtual Server exposes a number of interesting events which you may want to use to initiate appropriate actions. However, detecting these events with VBScript is not entirely intuitive. Below is a script that will listen for the first virtual machine state change event and then tell the user the name of the virtual machine for which the event occurred (state change refers to any action like power on, turn off, save state, etc…)
‘Jump to the main routine
‘ This subroutine makes sure that the script hangs around in order
‘ to capture any Virtual Server events
while (alive = 1)
alive = 0
alive = 1
‘Create Virtual Server COM object and indicate that all subroutines
‘that begin with “vs_” are event handlers
vs = WScript.CreateObject( “VirtualServer.Application” )
WScript.ConnectObject vs, “vs_”
As you can see a couple of things are needed:
- You need a ‘keep alive’ loop to make sure that the script doesn’t exit before the event fires.
- You need to use WScript.ConnectObject to associate Virtual Server events with a specific prefix. In this sample I use the ‘vs_’ prefix. This way whenever any event fires this script will check to see if it has a subroutine of form ‘vs_EVENTNAME’ – and if it has that subroutine it will run it. Details on all of the events that Virtual Server exposes can be found in the Virtual Server Programmers Guide.