JScript List and Property List

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 the name of the printer and the word "undefined" in brackets on each line (since the Status property does not exist in the p object). The question is: how can I list all available properties from p ? The usual method for (var i in p) {...} ...} does not work - it seems that the properties of the object p not listed.

Thanks in advance.

+6
wsh jscript
source share
3 answers
Operator

JScript for...in not compatible with WMI objects, because, moreover, they are more complex than native JScript objects. WMI objects expose their collection of properties using the special property Properties_ , so to list all the available properties of an object, you need to list this collection, for example, you list the results of a query to access individual WMI objects. Each property of an object is represented by a SWbemProperty object, which has a Name , Value and other properties that provide information about the corresponding object.

This example will help you understand:

 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); } } ") ExecQuery ( "SELECT Name, Status FROM Win32_Printer");. 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); } } colPrinters.moveNext ()) { 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); } } colProps.moveNext ()) { 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 also displays the value of the DeviceID property, because it is a key property of the Win32_Printer class, so it is also retrieved to uniquely identify instances of the class.

+10
source share

If you want to avoid having to use explicit Enumerator every time you need to sort through a collection object that he needs, you can define a little helper function as follows:

 function forEach(collection, func) { for (var e = new Enumerator(collection); !e.atEnd(); e.moveNext()) { func(e.item()); } } 

Collection iteration is becoming less awkward:

 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); }); }); p.Value); 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); }); }); 
+6
source share

I think the problem is in your request, you are only asking for a name. Try to indicate the name and status:

 var query = GetObject("winmgmts:").ExecQuery("SELECT Name, Status 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 + ")" ); } p.Status + ")"); var query = GetObject("winmgmts:").ExecQuery("SELECT Name, Status 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 + ")" ); } 

I got some more properties from http://msdn.microsoft.com/en-us/library/aa394363(VS.85).aspx and a few works. I asked for DriverName and Comment, and both had a text or even zero.

 var query = GetObject("winmgmts:").ExecQuery("SELECT Name, Status, DriverName, Comment 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 + ") " + p.DriverName + " " + p.Comment); } 
+1
source share

All Articles