Using WMI Scripting with Speech Server

Using WMI Scripting with Speech Server

This topic describes how Windows Management Instrumentation (WMI) scripting is used with Microsoft Speech Server (MSS).

Use WMI scripting to perform tasks such as saving and loading configuration settings, monitoring servers, starting and restarting services, or initiating an action when a server shuts down.

For information about administrative scripts, see Running Server Scripts.

For more information on scripting in Microsoft Windows, see Windows Script in the MSDN Library. For more information about WMI scripting, see the following topics in the MSDN Library:

  • WMI Scripting Primer: Part 1

  • WMI Scripting Primer: Part 2

Running MSS Sample Scripts

The scripts that ship with MSS are to be used as operations tools. The sample scripts are located in \Program Files\Microsoft Speech Server\Administrative Tools\Scripts.

Run scripts from the command line, or by double-clicking the script file in Windows Explorer. The following procedure describes how to run scripts from the command line.

To run sample scripts
  1. Click Start, click Run, type cmd and press ENTER.

  2. In the command window, browse to the folder \Program Files\Microsoft Speech Server\Administrative Tools\Scripts.

  3. To run a script, type cscript followed by the script file name, and press ENTER.

Writing and Running Custom Scripts

Use either VBScript or Windows JScript to write WMI scripts. For more information on VBScript and JScript, browse the MSDN Library online to the path Web Development\Scripting\Windows Script Technologies. See the separate sections on VBScript and JScript.

Notepad is a good authoring tool. The script must be saved as a text file with a .vbs extension. The following procedure provides some sample code, and steps through the mechanics of creating a valid VBScript file and running the script contained in the file.

Warning  Microsoft JScript .NET issues a security exception when partially trusted code attempts to create a Function object using either the eval or function constructs. For instructions on how to work around this issue, see this Microsoft Knowledge Base article.

To create and run a script
  1. Open Notepad.

  2. The following sample code will start the Calculator application. Copy the code into Notepad.

    set process = GetObject("winmgmts:{impersonationLevel=impersonate}!Win32_Process")
    

result = process.Create ("calc.exe",null,null,processid) WScript.Echo "Method returned result = " & result WScript.Echo "Id of new process is " & processid

  1. In Notepad, on the File menu, select Save As, enter StartNote.vbs as the file name,  and press ENTER.

  2. To run the script, click Start, click Run, type cmd and press ENTER.

  3. In the command window, browse to the folder containing StartNote.vbs, type cscript StartNote.vbs and press ENTER.

Scripting Common MSS Operations

The following sections provide examples of how WMI scripts can be used to perform common operations in administering MSS installations.

Reading and Writing Property Values

Use the Properties_ property of the SWbemObject object to access property values of MSS objects. Use the Put_ method to update a server instance with new property values. The following code sample demonstrates how this can be done. For more information on getting and setting property values, see Getting and Setting WMI Object Properties.

' Get telephony server object on specified host
Set objTelServer = GetObject("winmgmts:\\" & strHostname & "\root\MSS:TAS=@")

' Print configuration summary
For Each p in objTelServer.Properties_
    WScript.Echo p.Name, " = ", p.Value
Next

objTelServer.Properties_("ScriptTimeout")=5

'Update WMI with a modified instance
objTelServer.Put_
Starting and Stopping a Service

For information on using methods to stop and start a service, see Scripting Win32 Provider Methods.

Monitoring Service Status Changes

Events are real-time notifications that something of interest has changed in a WMI-managed resource. Use WMI scripting to subscribe to events that indicate a change to a WMI-managed resource. For example, it is possible to subscribe to an event that indicates when the amount of space on a logical disk drive drops below an acceptable threshold.

To monitor for changes in a service, use following query like this one, written in WMI Query Language:

' Format and issue a query for telephony server service change notification events.
strQuery = "SELECT * FROM __InstanceModificationEvent WITHIN 2 "
strQuery = strQuery & "WHERE TargetInstance ISA 'TAS'"

Create a sink object to receive event callbacks, and a subroutine to handle the events. The following code sample demonstrates a sink subroutine, and the statement creating the sink.

' Handler for WMI delivered service events.
Sub SINK_OnObjectReady(objWbemObject, objAsyncContext)
    Set t = objWbemObject.TargetInstance
    Set p = objWbemObject.PreviousInstance
    If p.State <> t.State Then
        WScript.Echo t.Path_.Server & ": " & Time & ": " & p.State & " -> " & t.State & " - " & t.Path_.Class
    End If
End Sub

' Connect to MSS namespace on the specified machine.
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objNamespace = objLocator.ConnectServer(strHostname, "root/MSS")

' Create a sink to process the service change events asynchronously.
Set objSink = WScript.CreateObject("WbemScripting.SWbemSink", "SINK_")

Use the SWbemServices.ExecNotifcationQueryAsync method to execute the event query and identify the event sink, as shown in the following statement.

' Execute query and await new events
objServices.ExecNotificationQueryAsync objSink, strQuery
See Also

Understanding WMI Scripting | Running Server Scripts | Windows Management Instrumentation Reference