Consider the following WSH snippet:
var query = GetObject("winmgmts:").ExecQuery("SELECT Name FROM Win32_Printer", "WQL", 0); var e = new Enumerator(query); for ( ; !e.atEnd(); e.moveNext ()) { var p = e.item(); WScript.Echo(p.Name + " (" + p.Status + ")"); }
It prints in every line a printer name and the word "undefined" in brackets (because Status
property isn't exist in p
object). The question is: how can I list all available properties from p
? The usual technique with for (var i in p) {...}
doesn't work--it seems that properties in p
object aren't enumerable.
Thanks in advance.
JScript's for...in
statement isn't compatible with WMI objects, because, well, they are more complex than native JScript objects. WMI objects expose their property collection via the special Properties_
property, so to list all available properties of an object, you need to enumerate this collection like you enumerate the query results to access individual WMI objects. Each object property is represented by a SWbemProperty
object that has the Name
, Value
and other properties providing info about the appropriate object property.
This example should help you get the idea:
var query = GetObject("winmgmts:").ExecQuery("SELECT Name, Status FROM Win32_Printer");
var colPrinters = new Enumerator(query);
var oPrinter, colProps, p;
// Enumerate WMI objects
for ( ; !colPrinters.atEnd(); colPrinters.moveNext()) {
oPrinter = colPrinters.item();
// Enumerate WMI object properties
colProps = new Enumerator(oPrinter.Properties_);
for ( ; !colProps.atEnd(); colProps.moveNext()) {
p = colProps.item();
WScript.Echo(p.Name + ": " + p.Value);
}
}
Note that this script will also display the DeviceID
property value, because it's a key property of the Win32_Printer
class, so it's also retrieved in order to uniquely identify class instances.
If you'd like to avoid the need to use an explicit Enumerator every time you need to iterate over a collection object that needs one, you can define a little helper function like this:
function forEach(collection, func) {
for (var e = new Enumerator(collection); !e.atEnd(); e.moveNext()) {
func(e.item());
}
}
Iteration over collections then becomes rather less clumsy:
var queryResult = GetObject("winmgmts:").ExecQuery("SELECT Name, Status FROM Win32_Printer");
// Enumerate WMI objects
forEach (queryResult, function (oPrinter) {
// Enumerate WMI object properties
forEach (oPrinter.Properties_, function (p) {
WScript.Echo(p.Name + ": " + p.Value);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With