WMI Scripting with Speech Server
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.
Running WMI Scripts
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
Click Start, click Run, type cmd, and then click OK.
In the Command Prompt window, browse to the folder containing the script you want to run.
At the command prompt, type script file name, and then press ENTER.
Writing and Running Custom Scripts
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.
Warning
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
Open Notepad.
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
On the File menu, click Save As, type StartCalc.vbs in the File name box, and then click Save.
Click Start, click Run, type cmd, and then click OK.
In the Command Prompt window, browse to the folder that contains StartCalc.vbs, type cscript StartCalc.vbs, and then press ENTER.
Scripting Common Speech Server Operations
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, "="
Else
Wscript.Echo p.Name, "=", Join(p.Value)
End If
Else
WScript.Echo p.Name, " = ", p.Value
End If
Next
objMSS.Properties_("ShutdownTimeout")=300
'Update WMI with a modified instance
objMSS.Put_
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