JScript Enumerator and list of properties

前端 未结 3 1068
南方客
南方客 2021-02-06 11:45

Consider the following WSH snippet:

var query = GetObject(\"winmgmts:\").ExecQuery(\"SELECT Name FROM Win32_Printer\", \"WQL\", 0);
var e = new Enumerator(query);
for         


        
3条回答
  •  终归单人心
    2021-02-06 12:20

    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.

提交回复
热议问题