Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort WMI query

Status
Not open for further replies.

greg0303

Technical User
May 5, 2006
94
CA
I am querying a server's WMI to find all the printers installed locally on it. This query returns all the printers, which I want:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinter = objWMIService.ExecQuery("Select * from Win32_Printer")

For Each objPrinter in colPrinter
msgbox objPrinter.caption
Next
However I want them in order of the printer name, so I tried putting in the query "Select * from Win32_Printer order by Caption" but this gave an error code 80041017 which suggests the caption field is null. Anyone know how to sort a WMI query?
 
Even though WQL has similarities to SQL, it is not the same. You can not have it return your collection in a sorted manner. You will need to use some alternate scripting technique to accomplish this; whether you use .NET array, disconnected recordset, bubble sort, or some other method.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Can you do
Code:
 Set colPrinter = objWMIService.ExecQuery("Select [!]Caption[/!] from Win32_Printer")

From what I've read, it should automatically put the list in alphabetical order.

<.

 
The script in faq329-4836 post works beautifully!

Thanks for your help.
 
No, that will not return it in an ordered fashion. That will just return the caption for any instances of the Win32_Printer class.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top