General Samples

The Microsoft Windows Script Host (WSH) is a tool that enables powerful scripting using languages such as VBScript and JScript. Using the scripting languages you already know you can now write scripts to automate common tasks, and to bind them to Application Center events. The following sample scripts have been provided with the Application Center documentation to help illustrate the process of scripting by way of example.

Directory Listing Sample

The Directory Listing sample script demonstrates how to use the WSH to gather useful information. This script can be attached to any of the numerous events in Application Center, and upon activation, reports the available space on each physical drive. In addition, the script prompts you to create a set of text files that lists all files and directories for each physical drive. Although not directly related to Application Center, the following script is provided here to help illustrate the process of writing a script for use with an Application Center event.

DirList.js (JScript)

var objShell = new ActiveXObject("WScript.Shell");


for (fsoSys = new Enumerator (GetObject("winmgmts:").ExecQuery
    ("select FreeSpace,Size,Name from WIN32_LogicalDisk where drivetype = 3"));
    !fsoSys.atEnd(); fsoSys.moveNext ())
{
    var fsoDrive = fsoSys.item ();

    WScript.Echo ("Space Available: " + fsoDrive.FreeSpace);
    var strFile = fsoDrive.Name
    objShell.Run("cmd /C tree /F /A " + fsoDrive.Name + 
        "\\ > " + strFile.substring(0,1) + "-dir.txt", 0);
}

To Use

From Notepad, or another text editor, cut and paste the above code and name the file DirList.js. You can cause Health Monitor to run this file, through the use of an action, when a specified threshold is reached. For more information on Monitoring, see Using Health Monitor in Application Center, Using Monitoring, and Thresholds and Actions.

Offline Events Sample

OffEvent.vbs (VBScript)

The Offline Events sample script demonstrates how to synchronously catch the first 10 server offline events. The script uses the WMI ExecNotificationQuery method to select the fields in the MicrosoftAC_Cluster_LoadBalancing_ServerOffline_Event class. The script then uses the ServerName, TimeGenerated, and DrainTime fields to return information back about the offline event. After 10 server events, the script terminates.

Dim objEvents, objOfflineEvent

Set objEvents=GetObject("winmgmts:root\MicrosoftApplicationCenter") _
    .ExecNotificationQuery ("select * from " & _
    "MicrosoftAC_Cluster_LoadBalancing_ServerOffline_Event ") 

WScript.Echo "Waiting for Server to go offline..."
For n = 1 to 10
    Set objOfflineEvent=objEvents.NextEvent
    WScript.Echo  "Server " & objOfflineEvent.ServerName & _
        " was taken offline at " & objOfflineEvent.TimeGenerated
    WScript.Echo " with a drain time of " & objOfflineEvent.DrainTime
Next

To Use

From Notepad, or another text editor, cut and paste the above code and save the file as OffEvent.vbs. You can run this file by either double-clicking it, or executing it from the command shell with cscript.exe or wscript.exe.

Sync Events Sample

SyncEvt.vbs (VBScript)

The Sync Events sample script demonstrates how to synchronously catch the first 10 server online and offline events. The script uses the WMI ExecNotificationQuery method to select the fields in the MicrosoftAC_Cluster_LoadBalancing_Event class. The script then uses the EventID, ServerName, and the NextEvent fields to return information back about the event. After 10 server events, the script terminates.

Dim strState, intCount, objEvents

Set objEvents = GetObject("winmgmts:root\MicrosoftApplicationCenter") _
    .ExecNotificationQuery ("select * from  " & _ 
    "MicrosoftAC_Cluster_LoadBalancing_Event where " & _
    "EventId = 4015 or EventId = 4016") 

WScript.Echo "Waiting for Server on/offline..."

intCount = 0
While intCount < 10
    Set NTEvent = objEvents.NextEvent
    If NTEvent.EventId = 4015 Then
        strState = "put online"
    ElseIf NTEvent.EventId = 4016 Then	
	    strState = "taken offline"
    End If
    WScript.Echo  "Server " & NTEvent.ServerName & " was " & strState 
    intCount = intCount + 1
Wend

To Use

From Notepad, or another text editor, cut and paste the above code and save the file as SyncEvt.vbs. You can run this file by either double-clicking it, or executing it from the command shell with cscript.exe or wscript.exe.

Did you find this information useful? Please send your suggestions and comments about the documentation to acdocs@microsoft.com.