WMI Scripting with Speech Server
The document is archived and information here might be outdated

WMI Scripting with Speech Server

Speech Server 2007

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Use Windows Management Instrumentation (WMI) scripting with Speech Server to perform tasks such as saving and loading configuration settings, monitoring servers, starting and restarting services, and initiating an action when a server shuts down.

Run scripts from the command line or by double-clicking the script file in Windows Explorer.

To run a WMI script from the command line
  1. Click Start, click Run, type cmd, and then click OK.

  2. In the Command Prompt window, browse to the folder containing the script you want to run.

  3. At the command prompt, type script file name, and then press ENTER.

Use VBScript or JScript to write WMI scripts. For more information about VBScript and JScript, see MSDN Scripting.

Scripts must be saved as a text file with a .vbs file name extension. The following procedure provides sample code and directions for creating a valid VBScript file and running the script contained in the file.

Microsoft JScript .NET issues a security exception when partially trusted code attempts to create a Function object by using either the eval or function constructs. For information about how to resolve this issue, see Microsoft Knowledge Base.

To create and run a script
  1. Open Notepad.

  2. Copy the following sample code into Notepad.

    The code is designed to start the Calculator application.

    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
  3. On the File menu, click Save As, type StartCalc.vbs in the File name box, and then click Save.

  4. Click Start, click Run, type cmd, and then click OK.

  5. In the Command Prompt window, browse to the folder that contains StartCalc.vbs, type cscript StartCalc.vbs, and then press ENTER.

The following code samples show how to use WMI scripting to perform common operations in administering Speech Server deployments.

To Read and Write Property Values

Use the Properties_ property of the SWbemObject object to access property values of Speech Server objects. The following code uses the Put_ method to update a server instance with new property values. For more information, see How to: Get and Set WMI Object Properties.

' Get MSS object on specified host
strHostname = "."
Set objMSS = GetObject("winmgmts:\\" & strHostname & "\root\MSSV2:MSS=@")
' Print configuration summary
For Each p in objMSS.Properties_
   If p.IsArray Then
      If isNull(p.Value) Then
        Wscript.Echo p.Name, "="
        Wscript.Echo p.Name, "=", Join(p.Value)
      End If
      WScript.Echo p.Name, " = ", p.Value
   End If


'Update WMI with a modified instance

To Start and Stop a Service

For information about using methods to stop and start a service, see How to: Script Win32 Provider Methods.

To Monitor 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 a query similar to the following query that is written in WMI Query Language.

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

Create a sink object to receive event callbacks and a subroutine to handle the events. The following code shows 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 Speech Server namespace on the specified machine.
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objServices = objLocator.ConnectServer(strHostname, "root/MSSV2")

' 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 this statement.

' Execute query and await new events
objServices.ExecNotificationQueryAsync objSink, strQuery
© 2016 Microsoft