Getting and Setting WMI Object Properties

Getting and Setting WMI Object Properties

This topic describes reading and writing Windows Management Instrumentation (WMI) object property values, including getting and setting property and qualifier values, and loading and saving configurations.

Getting and Setting Properties on MSS WMI Objects

Use properties to provide information about the characteristics of WMI objects. To see a list of the properties of a class for Microsoft Speech Server (MSS), see the reference topic on that class. To read property values, use code like the following statements.

Note  All MSS classes inherit from the SWbemObject object, which explains why the SWbemObject.Properties_ property is available in the following sample.

' Read each member of SWbemObject.Properties_
For Each p in objTelServer.Properties_
    ' Display the values of the Name and Value properties of the SWbemProperty object
    WScript.Echo p.Name, " = ", p.Value
Next

Use the SWbemObject.Properties_ property to read and write property values of MSS objects. In the sample below, objTelServer is an SWbemObject object variable representing a server object in MSS.

For Each p In objTelServer.Properties_
    ' Get the default value
    v = p.Qualifiers_("DefaultValue")
    ' If the current value is not the default, then set it
    If p.Value <> v Then
        p.Value = v
    End If
Next

Reading Qualifiers on MSS WMI Objects

A qualifier is a tag that provides additional information about a WMI object, method, or property. Access the qualifiers of a WMI object or property through the SWbemObject.Qualifiers_ property, as in the following sample code.

Set p = objTelServer.Properties_(strProperty)

If strNewValue <> "" Then
    'p is a TAS property variable
    p.Value = strNewValue
Else
    d = p.Qualifiers_("DefaultValue")
    p.Value = d
End If

Loading and Saving Configurations

To load values from a configuration file, use the VBScript Split function and the FileSystemObject to parse the file and read property values. Use the SWbemObject Properties_ property to store the values read from the configuration file, and use the SWbemObject Put_ method to update the server instance with those values.

For Each line in Split(f.ReadAll, vbCrLf)

    ' Omit empty lines and comments
    ' ...
    ' Split line into name and value parts
    ' ...

        Set p = objTelServer.Properties_(strName)
        If Not Err Then

            If p.IsArray Then
                p.Value = Join(strValue)
            Else
                p.Value = strValue
            End If

            If Err Then
                WScript.Echo "Failed to assign " & strName & ": " & Err.Description : Err.Clear
            End If

        Else
            WScript.Echo "No such property: " & strName : Err.Clear
        End If
Next

' Update WMI with modified instance
objTelServer.Put_

To save a server configuration to a file, use the SWbemObject Properties_ property to provide the collection of properties on the server. Use the VBScript WriteLine method to write property values to a file, as in the following sample.

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile(strFilename, true)

' Write all configuration settings
For Each p In objTelServer.Properties_
    If p.IsArray Then
        f.WriteLine p.Name & ":" & Join(p.Value)
    Else
        f.WriteLine p.Name & ":" & p.Value
    End If
Next