6

Given a query such as

SELECT * FROM WIN32_PROCESS
  1. Is there a way to interrogate the result object for the names of the columns returned?
  2. Write all the rows in the result object to a text file, say
RRUZ
  • 134,889
  • 20
  • 356
  • 483
AAsk
  • 1,431
  • 4
  • 17
  • 25

1 Answers1

10

Is there a way to interrogate the result object for the names of the columns returned?

Yes. Each WMI object has the Properties_ collection that provides information about that object's properties. To get the names of properties available in an object, enumerate the Properties_ collection and check each item's Name.

Write all the rows in the result object to a text file, say

Enumerate all the rows and use the FileSystemObject to write them to the desired text file. Pseudocode:

create a text file and open it for writing

for each object in the result set
  for each property in the object
    write the property value to the file

close the file


Alternatively, you could use wmic to do all the work for you:

wmic /output:e:\processes.txt process get /all
wmic /output:e:\processes.csv process get /all /format:csv
Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thanks Helen. Using your pseudo code as a basis, I can write the names of the columns & the values - the commandline code (sample) is very useful. – AAsk Mar 09 '11 at 16:15
  • 1
    for anybody who runs into this in the future, the output flag goes before the alias. wmic /output:c:\processes.csv process get /all /format:csv and the resulting csv is not read automatically by excel, you have to specify the delimiter for it to look right.. – John Moses Jan 08 '14 at 21:22
  • When I use this command (on Windows 7 64-bit) I get "Invalid XSL format (or) file name." Server 2003 has the CSV format available, though. Is it possible to get CSV on Windows 7? – Paul Moore Feb 07 '14 at 16:32
  • @PaulMoore: Works for me on Windows 7 64-bit. Does [this](http://stackoverflow.com/q/9673057/113116) help? – Helen Feb 11 '14 at 06:37
  • @Helen yes, that command works, thanks. Of course, getting the syntax right in powershell (which I was using) is a nightmare, but you don't really need WMIC in powershell so that's not such an issue :-) – Paul Moore Feb 11 '14 at 09:44