Getting Information about a Variable in VBScript
Since VBScript uses variants, it can be useful to find out what type of information ended up in a variable. Here is a simple subroutine to call all the built-in functions that return information about a variable.
Sub GetVarInfo(varname)
Wscript.Echo ""
Wscript.Echo "VarType = " & VarType(varname)
Wscript.Echo "TypeName = " & TypeName(varname)
Wscript.Echo "IsArray = " & IsArray(varname)
Wscript.Echo "IsDate = " & IsDate(varname)
Wscript.Echo "IsEmpty = " & IsEmpty(varname)
Wscript.Echo "IsNull = " & IsNull(varname)
Wscript.Echo "IsNumeric = " & IsNumeric(varname)
Wscript.Echo "IsObject = " & IsObject(varname)
Wscript.Echo "Count = " & varname.count
Wscript.Echo ""
End Sub
Say you do the following in your script (you'll need to include the code above too if you actually wanted to run this on your own):
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
Set colOperatingSystem = objWMIService.ExecQuery("SELECT Caption FROM Win32_OperatingSystem")
GetVarInfo(strComputer)
GetVarInfo(objWMIService)
GetVarInfo(colProcesses)
Here is the resulting output from GetVarInfo(strComputer):
VarType = 8
TypeName = String
IsArray = False
IsDate = False
IsEmpty = False
IsNull = False
IsNumeric = False
IsObject = False
That is as simple as it gets. It is a string that is not any of the following - array, date, empty, null, numeric, object or null.
Here is the GetVarInfo(objWMIService) output:
VarType = 9
TypeName = SWbemServicesEx
IsArray = False
IsDate = False
IsEmpty = False
IsNull = False
IsNumeric = False
IsObject = True
The TypeName function will return something different depending on the type of object it is. In this case it knows enough to tell me it is a SWbemServicesEx object. In other cases it might return Object for a generic object, Unknown for an unknown object, or Nothing for an object variable that does not yet refer to an object instance. Since Count is only valid for collections, that line generates an error but the script continues because On Error Resume Next is included at the beginning of the script.
Here is the output from GetVarInfo(colProcesses):
VarType = 9
TypeName = SWbemObjectSet
IsArray = False
IsDate = False
IsEmpty = False
IsNull = False
IsNumeric = False
IsObject = True
Count = 71
A SWbemObjectSet is a collection of WMI objects. Since it is a collection, the count of items in the collection is displayed.