Previous Page
Next Page

Testing Scripting Interface

After we have checked for local and for remote WMI functionality, we may need to test the scripting interface. To do this, we will need to check both the core WMI provider and the provider host interface. We can use a script to do this. In the RetrieveWMISettings.vbs script, we use the connectServer method from the SWbemLocator object. The reason for doing this is the SWbemLocator object is already set up for us to specify alternative connections on a remote computer. This will allow a fuller range of tests. We connect to the WIN32_WMISetting class. There is only one instance of the WIN32_WMISetting class. We can use the ampersand to enable us to get the one instance of the WIN32_WMISetting class that represents the WMI settings for the computer. After we have executed our query, we use the GetObjectText_ method, which will retrieve all the properties in the class as well as the values assigned to those properties. The output text will be in Managed Object Format (MOF) format. The MOF format provides an easy way to create and register items into the WMI repository. We cannot specify any modifiers for this method. The input flag is optional. If you choose to specify the input flag, you must supply a zero, because 0 is the only allowed value for this flag. Specifying the input flag will not change the way the method operates, so we leave it off in the RetrieveWMISettings.vbs script. The Header information section of the script is left out for clarity purposes.

RetrieveWMISettings.vbs

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Win32_WMISetting=@"
strUsr =""'Blank for current security. Domain\Username
strPWD = ""'Blank for current security.
strLocl = "MS_409" 'US English. Can leave blank for current language
strAuth = ""'If specify domain in strUsr this must be blank
iFlag = "0" 'Only two values allowed here: 0 (wait for connection) 128 (wait max two min)

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, _
 wmiNS, strUsr, strPWD, strLocl, strAuth, iFLag)
Set objItem = objWMIService.get(wmiQuery)

WScript.Echo objItem.GetObjectText_

If the RetrieveWMISettings.vbs script works, you have successfully tested the core WMI functionality. You have not, however, tested other WMI providers, only the WbemCore provider. The RetrieveComputerSystem.vbs script uses the WIN32_ComputerSystem class. The WIN32_ComputerSystem class relies on the CIMWin32 provider, and a query to this class will exercise an extremely important WMI provider. We specify the name of the computer in the strComputer variable, but because we want to use the Get method, we need to specify a particular instance of the WIN32_ComputerSystem class, which happens to be the local machine. When we use the WMI moniker to make a WMI connection, we do not supply the computer name in single quotation marks. We contain it in a variable called strComputer, which is delimited by double quotation marks. When we supply a computer name for the Key Name property of the WIN32_ComputerSystem class, WMI wants the computer name to be embedded inside single quotation marks. The use of quotation marks when supplying values to WMI is not consistent. At times numbers do not need quotation marks, but strings do. It is best to use quotation marks, and if it fails, then remove them. To use a single variable for these two uses, which have different requirements, we devised the simple funFix function and included it at the bottom of the script. All it does is take the string that is supplied to it, append a single quotation mark as both a prefix and as a suffix, and assign the resultant string to the function name. This allows dual use of the same variable name.

RetrieveComputerSystem.vbs

strComputer = "London" 'name of the target computer system
wmiNS = "\root\cimv2"
wmiQuery = "win32_ComputerSystem.name=" & funFix(strComputer)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set objItem = objWMIService.get(wmiQuery)

  Wscript.Echo myFun(wmiQuery) & objItem.getObjectText_

Function myFun(input)
Dim lstr
lstr = Len(input)
myFun = input & vbcrlf & string(lstr,"=")
End Function

Function funFix(strIN) 'computer name needs single '
funFix = "'" & strIN & "'"
End function


Previous Page
Next Page