1.4 Helpers for the discovery


1.4 Helpers for the discovery

Discovering the class capabilities and the instances they provide is a long process. For this, you can use WMI CIM Studio or scripts such as those shown in Sample 1.4 ("Listing a single instance of a class with its properties formatted") and Sample 1.5 ("Listing all instances of a class with their properties formatted"). These samples expose some command-line parameters (class name, namespace, userid, and password) to be more generic and usable remotely. The next script, helping us in our discovery and called GetSingleInstance.Wsf, exposes the following command-line parameters:

 C:\>GetSingleInstance.Wsf Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996–2001. All rights reserved. Usage: GetSingleInstance.wsf WMIclass WMIInstanceName [/Machine:value] [/ User:value] [/Password:value] [/NameSpace:value] Options: WMIclass : the WMI class to use. WMIInstanceName : the WMI instance name. Machine : determine the WMI system to connect to. (default=LocalHost) User : determine the UserID to perform the remote connection. (default=none) Password : determine the password to perform the remote connection. (default=none) NameSpace : determine the WMI namespace to connect to. (default=Root\CIMv2) 

Sample 1.4: Listing a single instance of a class with its properties formatted

start example

  1:<?xml version="1.0"?>  .:  8:<package>  9: <job> ..: 13:  <runtime> ..: 20:  </runtime> 21: 22:  <script language="VBScript" src="/books/2/679/1/html/2/.\Functions\DisplayFormattedPropertyFunction.vbs" /> 23:  <script language="VBScript" src="/books/2/679/1/html/2/.\Functions\TinyErrorHandler.vbs" /> 24: 25:  <object prog  reference="true"/> 26:  <object prog  /> 27: 28:  <script language="VBscript"> 29:  <![CDATA[ ..: 33:  Const cComputerName = "LocalHost" 34:  Const cWMINameSpace = "root/cimv2" ..: 49:  ' -------------------------------------------------------------------------------- 50:  ' Parse the command line parameters 51:  If WScript.Arguments.Unnamed.Count = 0 Or WScript.Arguments.Unnamed.Count = 1 Then 52:     WScript.Arguments.ShowUsage() 53:     WScript.Quit 54:  Else 55:     strWMIclass = WScript.Arguments.Unnamed.Item(0) 56:     strWMIInstance = WScript.Arguments.Unnamed.Item(1) 57:  End If 58: 59:  strUserID = WScript.Arguments.Named("User") 60:  If Len(strUserID) = 0 Then strUserID = "" 61: 62:  strPassword = WScript.Arguments.Named("Password") 63:  If Len(strPassword) = 0 Then strPassword = "" 64: 65:  strComputerName = WScript.Arguments.Named("Machine") 66:  If Len(strComputerName) = 0 Then strComputerName = cComputerName 67: 68:  strWMINameSpace = WScript.Arguments.Named("NameSpace") 69:  If Len(strWMINameSpace) = 0 Then strWMINameSpace = cWMINameSpace 70:  strWMINameSpace = UCase (strWMINameSpace) 71: 72:  objWMILocator.Security_.AuthenticationLevel = wbemAuthenticationLevelDefault 73:  objWMILocator.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate 74: 75:  Set objWMIServices = objWMILocator.ConnectServer(strComputerName, strWMINameSpace, _ 76:                           strUserID, strPassword) ..: 79:  Set objWMIInstance = objWMIServices.Get (strWMIclass & "=" & strWMIInstance) ..: 82:  Set objWMIPropertySet = objWMIInstance.Properties_ 83:  For Each objWMIProperty In objWMIPropertySet 84:   DisplayFormattedProperty objWMIInstance, _ 85:                  objWMIProperty.Name, _ 86:                  objWMIProperty.Name, _ 87:                  Null 88:  Next ..: 94:  ]]> 95:  </script> 96: </job> 97:</package> 

end example

Sample 1.5: Listing all instances of a class with their properties formatted

start example

   1:<?xml version="1.0"?>   .:   8:<package>   9:  <job>  ..:  13:    <runtime>  ..:  19:    </runtime>  20:  21:    <script language="VBScript" src="/books/2/679/1/html/2/.\Functions\DisplayFormattedPropertyFunction.vbs" />  22:    <script language="VBScript" src="/books/2/679/1/html/2/.\Functions\TinyErrorHandler.vbs" />  23:  24:    <object prog  reference="true"/>  25:    <object prog  />  26:  27:    <script language="VBscript">  28:    <![CDATA[  ..:  32:    Const cComputerName = "LocalHost"  33:    Const cWMINameSpace = "root/cimv2"  ..:  72:    Set objWMIServices = objWMILocator.ConnectServer(strComputerName, strWMINameSpace, _  73:                                                     strUserID, strPassword)  74:    If Err.Number Then ErrorHandler (Err)  75:  76:    Set objWMIInstances = objWMIServices.InstancesOf (strWMIclass)  77:    If Err.Number Then ErrorHandler (Err)  78:  79:    If objWMIInstances.Count Then  80:       For Each objWMIInstance In objWMIInstances  81:           WScript.Echo  82:           Set objWMIPropertySet = objWMIInstance.Properties_  83:           For Each objWMIProperty In objWMIPropertySet  84:               DisplayFormattedProperty objWMIInstance, _  85:                                        objWMIProperty.Name, _  86:                                        objWMIProperty.Name, _  87:                                        Null  88:           Next  89:           Set objWMIPropertySet = Nothing  90:       Next  91:    Else  92:       WScript.Echo "No instance."  93:    End If  ..:  98:    ]]>  99:    </script> 100:  </job> 101:</package> 

end example

The listing of the script is given for information purposes only, since it is pretty simple.

Sample 1.5 uses the same structure as Sample 1.4. Only the core code logic is displayed. The command-line parameters are:

 C:\>GetCollectionOfInstances.Wsf Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996–2001. All rights reserved. Usage: GetCollectionOfInstances.wsf WMIclass [/Machine:value] [ User:value] [/Password:value] [/NameSpace:value] Options: WMIclass  : the WMI class to use. Machine  : determine the WMI system to connect to. (default=LocalHost) User  : determine the UserID to perform the remote connection. (default=none) Password  : determine the password to perform the remote connection. (default=none) NameSpace : determine the WMI namespace to connect to. (default=Root\CIMv2) 

Instead of using the DisplayInstanceProperties.vbs script containing the DisplayInstanceProperties() function (see Sample 4.31 in the appendix), these scripts use the DisplayFormattedPropertyFunction() contained in the DisplayFormattedPropertyFunction.vbs script (line 13). This function has the exact same purpose as DisplayInstanceProperties.vbs developed previously, but it displays the property content in a different way. As an example:

  1:    C:\>GetSingleInstance Win32_Service "'SNMP'"  2:    Microsoft (R) Windows Script Host Version 5.6  3:    Copyright (C) Microsoft Corporation 1996–2001. All rights reserved.  4:  5:    AcceptPause: ............................. FALSE  6:    AcceptStop: .............................. FALSE  7:    Caption: ................................. SNMP Service  8:    CheckPoint: .............................. 0  9:    CreationclassName: ....................... Win32_Service 10:    Description: ............................. Enables Simple Network Management Protocol (SNMP)                                                   requests to be processed by this computer. If                                                   this service is stopped, the computer will be                                                   unable to process SNMP requests. If this service                                                   is disabled, any services that explicitly depend                                                   on it will fail to start. 11:    DesktopInteract: ......................... FALSE 12:    DisplayName: ............................. SNMP Service 13:    ErrorControl: ............................ Normal 14:    ExitCode: ................................ 1077 15:    *Name: ................................... SNMP 16:    PathName: ................................ J:\WINDOWS\System32\snmp.exe 17:    ProcessId: ............................... 0 18:    ServiceSpecificExitCode: ................. 0 19:    ServiceType: ............................. Own Process 20:    Started: ................................. FALSE 21:    StartMode: ............................... Manual 22:    StartName: ............................... LocalSystem 23:    State: ................................... Stopped 24:    Status: .................................. OK 25:    SystemCreationclassName: ................. Win32_ComputerSystem 26:    SystemName: .............................. XP-DPEN6400 27:    TagId: ................................... 0 28:    WaitHint: ................................ 0 

This display formatting is easier to read and can be used to display instance information in a manner similar to some command-line utilities.

The DisplayFormattedPropertyFunction.vbs accepts four parameters, as follows:

  • The WMI instance object (line 38)

  • Any string type that can be used as a label for the property (line 39)

  • The property name to extract the value from (line 40)

  • A second property name that could be displayed on the same line as the first property (line 41)

When set to null, the second property is not displayed. However, the function does not display the property syntax. In the same way, the property is skipped if its value is null. Moreover, if the property is an array, the function behaves accordingly and transparently. This function will greatly help and ease the display of any class properties for the next samples. The DisplayFormattedPropertyFunction.vbs code is given in Sample 1.6 for reference. The WMI scripting technique used was covered in Understanding WMI Scripting, Chapter 4.

Sample 1.6: The DisplayFormattedPropertyFunction.vbs function.:

start example

   .:   6:' ----------------------------------------------------------------------------------------   7:Function DisplayFormattedProperty (objWMIInstance, strText, strProperty1, strProperty2)  ..:  16:    If Not IsNull (strProperty1) Then  17:       varValue1 = objWMIInstance.Properties_.Item (strProperty1)  18:       If Err.Number Then  19:          varValue1 = strProperty1  20:          Err.Clear  21:       Else  22:          If Ucase (strProperty1) = "TIME_CREATED" Then  23:             objWMIDateTime.SetFileTime (varValue1)  24:             varValue1 = objWMIDateTime.GetVarDate (True) & " (" & objWMIDateTime.Value & ")"  25:          Else  26:               Select Case objWMIInstance.Properties_.Item (strProperty1).CIMType  27:                      Case wbemCimtypeDatetime  28:                         objWMIDateTime.Value = varValue1  29:                         varValue1 = objWMIDateTime.GetVarDate (False)  30:                    Case wbemCimtypeBoolean  31:                         varValue1 = Ucase (varValue1)  32:                    Case wbemCimtypeObject  33:                         varValue1 = "<OBJECT>"  34:             End Select  35:          End If  36:  37:          boolCIMKey = _  38:              objWMIInstance.Properties_.Item (strProperty1).qualifiers_.Item("key").Value  39:          If Err.Number Then  40:             Err.Clear  41:             boolCIMKey = False  42:          End If  43:          If boolCIMKey Then  44:             If Mid (strText, 1, 1) = Chr (32) Then  45:                intSpace = Len (strText) - Len(LTrim(strText))  46:                strText = Space(intSpace) & "*" & Ltrim(strText)  47:             Else  48:                strText = "*" & strText  49:             End If  50:          End If  51:       End If  52:    Else  53:       varValue1 = strProperty1  54:    End If  55:  56:    If Not IsNull (strProperty2) Then  57:       varValue2 = objWMIInstance.Properties_.Item (strProperty2)  58:       If Err.Number Then  59:          varValue2 = strProperty2  60:          Err.Clear  61:       Else  62:          If Ucase (strProperty2) = "TIME_CREATED" Then  63:             objWMIDateTime.SetFileTime (varValue2)  64:             varValue2 = objWMIDateTime.GetVarDate (True) & " (" & objWMIDateTime.Value & ")"  65:          Else  66:             Select Case objWMIInstance.Properties_.Item (strProperty2).CIMType  67:                    Case wbemCimtypeDatetime  68:                         objWMIDateTime.Value = varValue2  69:                         varValue2 = objWMIDateTime.GetVarDate (False)  70:                    Case wbemCimtypeBoolean  71:                         varValue2 = Ucase (varValue2)  72:                    Case wbemCimtypeObject  73:                         varValue2 = "<OBJECT>"  74:             End Select  75:          End If  76:       End If  77:    Else  78:       varValue2 = strProperty2  79:    End If  80:  81:    If Not IsNull (varValue1) Then  82:       If Not IsNull (varValue2) Then  83:          If IsArray (varValue1) Then  84:             For intIndice=0 To UBound(varValue1)  85:                 If intIndice = 0 Then  86:                    WScript.Echo strText & ": " & string (40 - Len(strText), ".") & _  87:                                 " " & varValue1 (intIndice) & _  88:                                 " " & varValue2 (intIndice)  89:                 Else  90:                    WScript.Echo Space (42) & _  91:                                 " " & varValue1 (intIndice) & _  92:                                 " " & varValue2 (intIndice)  93:                 End if  94:             Next  95:         Else  96:             WScript.Echo strText & ": " & string (40 - Len(strText), ".") & _  97:                          " " & varValue1 & _  98:                          " " & varValue2  99:          End IF 100:       Else 101:          If IsArray (varValue1) Then 102:             For intIndice=0 To UBound(varValue1) 103:                 If intIndice = 0 Then 104:                    WScript.Echo strText & ": " & string (40 - Len(strText), ".") & _ 105:                                 " " & varValue1 (intIndice) 106:                 Else 107:                    WScript.Echo Space (42) & _ 108:                                 " " & varValue1 (intIndice) 109:                 End if 110:             Next 111:          Else 112:             WScript.Echo strText & ": " & string (40 - Len(strText), ".") & _ 113:                          " " & varValue1 114:          End IF 115:       End IF 116:    End IF 117: 118:    Err.Clear 119: 120:End Function 

end example

For now, we have four scripts that can be used as utilities to help our understanding and WMI discovery:

  • LoadCIMinXL.wsf: a Windows script file self-documenting the classes available from the CIM repository in an Excel sheet (see Sample 4.32 in the appendix).

  • LocateProviders.wsf: to locate the WMI provider registration instances with the supported classes (Samples 1.1 through 1.3).

  • GetSingleInstance.wsf: to list a single instance of a class with its properties formatted (Sample 1.4).

  • GetCollectionOfInstances.wsf: to list all instances of a class with their properties formatted (Sample 1.5).

With the help of these scripts, our next purpose is to develop small utilities based on the WMI scripting techniques and the class capabilities.




Leveraging WMI Scripting
Leveraging WMI Scripting: Using Windows Management Instrumentation to Solve Windows Management Problems (HP Technologies)
ISBN: 1555582990
EAN: 2147483647
Year: 2003
Pages: 82
Authors: Alain Lissoir

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