Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export all rows of a WMI query to a file?

Tags:

wmi

wmi-query

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
like image 378
AAsk Avatar asked Mar 08 '11 07:03

AAsk


1 Answers

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
like image 80
Helen Avatar answered Sep 24 '22 12:09

Helen