Script Samples

We've included some scripts to illustrate how scripting, using the Windows Script Host, can be used in the Application Center environment to help you automate administrative tasks, as well as extend the product's feature set.

Script: Asynchronously Trap an Event

This script illustrates how you can asynchronously trap an Application Center event and write a notification to the screen. The event in this example is fired when a member goes offline and is no longer in the load-balancing loop. The script uses the Windows Management Instrumentation (WMI) method ExecNotificationQueryAsync to select the fields in the MicrosoftAC_Cluster_LoadBalancing_ServerOffline_Event class.

 Dim objService, objSink Set objService = GetObject("winmgmts:root\MicrosoftApplicationCenter") Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_") objService.Security_.ImpersonationLevel = 3 objService.ExecNotificationQueryAsync objSink, "SELECT * FROM " & _     "MicrosoftAC_CLuster_LoadBalancing_ServerOffline_Event " WScript.Echo "Waiting for events." Sub SINK_OnCompleted(iHResult, objErrorObject, objAsyncContext)     WScript.Echo "Asynchronous operation is done." End Sub Sub SINK_OnObjectReady(objObject, objAsyncContext)     WScript.Echo "Server " & objObject.ServerName & " taken offline" End Sub 

To Use This Script

Using Notepad, or another text editor, copy and paste the preceding code into a new file, and then save the file as AsyncEvents.vbs. You can run this script by either double-clicking the file name or by executing it from the command line with cscript.exe AsyncEvents.vbs or wscript.exe AsyncEvents.vbs.

Script: Synchronously Trap an Event

This script illustrates how you can synchronously trap an Application Center event and write a notification to the screen. The event in this example is fired when a member goes offline and is no longer in the load-balancing loop. The script uses the WMI method ExecNotificationQuery 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 about the offline event. After 10 server events, the script terminates.

 Dim intEvent, objEvents, evtEvent Set objEvents = GetObject("winmgmts:root\MicrosoftApplicationCenter") _     .ExecNotificationQuery ("select * " & _     "from MicrosoftAC_CLuster_LoadBalancing_ServerOffline_Event ")  WScript.Echo "Waiting for Server to go offline..." For intEvent = 1 to 10     Set evtEvent = objEvents.NextEvent     WScript.Echo  "Server " & evtEvent.ServerName & _         " was " & "taken offline" & " at " & OfflineEvent.TimeGenerated     WScript.Echo "with a drain time of " & evtEvent.DrainTime Next 

To Use This Script

Using Notepad, or another text editor, copy and paste the preceding code into a new file, and then save the file as GetOfflineEventInfo.vbs. You can run this script by either double-clicking the file name or by executing it from the command line with cscript.exe GetOfflineEventInfo.vbs or wscript.exe GetOfflineEventInfo.

Script: Obtain the Available Disk Space and Directory Listing

This script is used in a custom action (Script) that's associated with the data collector LogicalDisk.

When the data collector crosses its threshold, which is based on the amount of free space (less than 10 percent), the custom action is triggered and this script prepares a notification about the amount of available disk space. In addition to displaying a message about the available space, the script also provides a prompt that asks if you want to see a directory listing.

 im fsoFSys, fsoDrive, strName Set objShell= CreateObject ("WScript.Shell") Set fsoFSys = GetObject("winmgmts:").ExecQuery("select FreeSpace,Size,Name from  WIN32_LogicalDisk where drivetype = 3")      For Each fsoDrive In fsoFSys              WScript.Echo "Space available: " & fsoDrive.FreeSpace        if MsgBox ("Directory listing?", vbYesNo) = vbYes Then         objShell.Run "cmd /C dir " & fsoDrive.Name & "\  >" & Left(fsoDrive.Name,1) & "-dir.txt",0    End If                  Next 

To Use This Script

Using Notepad, or another text editor, copy and paste the preceding code into a new file, and then save the file as DiskSpace.vbs. Specify this script in a custom action, and then associate this action with a data collector.

You can run this script manually by either double-clicking the file name or by executing it from the command line with cscript.exe DiskSpace.vbs or wscript.exe DiskSpace.vbs.

Script: Save ACLog Database as CSV Text File

This script establishes a connection to the specified database, extracts all the records, and writes the records, with comma-separated fields, to a text file. You can have this script associated with a data collector that either has a threshold based on the size of ACLog, or is date- and time-based.

 Dim fsoOutput, fsoStream, objConnection, objRecords Dim strName, strFields Set fsoOutput = CreateObject("Scripting.FileSystemObject") Set objConnection = CreateObject("ADODB.Connection") objConnection.Open = "Provider = SQLOLEDB; Data Source = cfurlin0;" _     & "Initial Catalog = Pubs; User ID = sa; Password =;" Set objRecords = objConnection.Execute("select * from stores") For Each strName In objRecords.Fields     strFields = strFields + strName.Name + "," Next Set fsoStream = fsoOutput.CreateTextFile("output.csv", True) fsoStream.WriteLine Left(strFields, Len(strFields) - 1) fsoStream.WriteLine Replace(objRecords.GetString, vbTab, ",") objConnection.Close 

To Use This Script

Using Notepad, or another text editor, copy and paste the preceding code into a new file. Edit the file to provide the required data source, database name, user identifier, and password. Save the file as SQLToCSV.vbs. Specify this script in a custom action, and then associate this action with a data collector.

You can run this script manually by either double-clicking the file name or by executing it from the command line with cscript.exe SQLToCSV.vbs or wscript.exe SQLToCSV.vbs.

Script: Backup the ACLog Database

This next script is a variation of the SQLToCSV script. It also enables you to dump the contents of a member database to a text file. You can have this script associated with a data collector that either has a threshold based on the size of ACLog, or is date- and time-based.

Because Application Center uses a named instance of the Microsoft SQL Server 2000 Desktop Engine (SQL desktop engine) on each member, you must specify the named instance (for example, ACDW516AS\MSAC).

 ' '   DMOOpen Function _ Opens the database ' Function DMOOpen(Server)    Dim oServer, InstanceName    InstanceName = Server & "\MSAC"    Set oServer = CreateObject("SQLDMO.SQLServer")    oServer.LoginSecure = True    oServer.Connect InstanceName , "", ""    Set DMOOpen = oServer End Function ' '   DatabaseBackup _ Writes the data to a file ' Sub DatabaseBackup(Server, DBName, FileName)     Dim oServer, oBackup     Set oServer = DMOOpen(Server)     Set oBackup = CreateObject("SQLDMO.Backup")     oBackup.Action = 0 ' SQLDMOBackup_Database     oBackup.Database = DBName     oBackup.Files = FileName     oBackup.SQLBackup oServer End Sub 

To Use This Script

Using Notepad, or another text editor, copy and paste the preceding code into a new file. Save the file as DumpACLog.vbs. In DatabaseBackup(Server, DBName, FileName) provide values; for example, ACDW516AS, ACLog, and ACDW516AS.bak. Execute the file on the member whose database you want to archive. Specify this script in a custom action, and then associate this action with a data collector.

You can run this script manually by either double-clicking the file name or by executing it from the command line with cscript.exe DumpACLog.vbs or wscript.exe DumpACLog.vbs.

Batch File: Clean ACLog Tables

You can create a batch file to clean out the Application Center Events and Performance Logging database after you've archived the database on the local member. When you run this batch file, each command line in the file connects to one of the tables that constitute ACLog and truncates the table to clean out the existing data.

 @ECHO OFF IF "%1" == "" THEN GOTO HELP IF "%1"=="/?" THEN GOTO HELP Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE PerfHistory" Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE PerfHistory2" Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE PerfHistory3" Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE PerfHistory4" Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE PerfHistory5" Osql -E -d ACLOG -S %1\msac -Q "TRUNCATE TABLE Events" 

To Use This Batch File

Using Notepad, or another text editor, copy and paste the preceding code into a new file. Save the file as CleanACLog.bat. Execute the file on the member whose database you want to archive by running CleanACLog.bat from the command prompt.

Additional Samples

In addition to the preceding samples, the Resource Kit CD also contains a diverse collection of script samples. After you install the Resource Kit from the CD, you can access these samples by using the following default installation path:

C:\Program Files\Microsoft Application Center 2000 Resource Kit\ Command line scripts



Microsoft Application Center 2000 Resource Kit 2001
Microsoft Application Center 2000 Resource Kit 2001
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net