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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query for data from a WMI class

WMI

Query for data from a WMI class

by  ChrisRChamberlain  Posted    (Edited  )
If you need to recover data from a WMI class, the .prg posted in this FAQ will take the code generated by [link http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&DisplayLang=en]Microsoft's WMI Code Creator[/link] and output it to a .txt file.

No apologies for the bare bones utility style of the .prg - it is deliberately simple both in structure and use and offers developers the opportunity to make of it what they wish.

It's important to note that the version posted relies on the content of the Windows clipboard as a data source, but this can be easily changed so that the data source is a file.

Copy the code and paste into a .prg amending path(s) etc to suit and save to a suitable filename.

Start the WMI Code Creator, ensuring the Code Language selected is Visual Basic Script, and select a class from the combobox labelled 'Classes (dynamic or static):' on the 'Query for data from a WMI class tab' with the 'Namespace' combo showing "root\CIMV2".

Left click on the first item in the 'properties' listbox, scroll to the bottom of the list, depress the shift key and select the last item in the listbox, thus selecting all properties.

Click on the command button captioned 'Open code in Notepad'.

Press CTRL+A followed by CTRL+C and click on the command window in VFP.

Then 'DO wmidata2vfp' or whatever your utility .prg has been named.

You will find a report in .txt format, total instances to be found at the end of the file, will be generated by VFP.
Code:
[color blue]SET SAFETY OFF
SET DELETED ON

lcFile = [wmiscript.txt]
STRTOFILE(_CLIPTEXT,lcFile)
CREATE CURSOR csrCONVERT (ctext C(254))
APPEND FROM (lcFile) SDF

REPLACE csrCONVERT.ctext WITH STRTRAN(csrCONVERT.ctext,[Set ],[]) 	;
	FOR [GetObject(] $ csrCONVERT.ctext
REPLACE csrCONVERT.ctext WITH STRTRAN(csrCONVERT.ctext,[Set ],[]) 	;
	FOR [colItems] $ csrCONVERT.ctext
REPLACE csrCONVERT.ctext WITH STRTRAN(csrCONVERT.ctext,[ _],[ ;]) 	;
	ALL
REPLACE csrCONVERT.ctext WITH STRTRAN(csrCONVERT.ctext,[& ],[<<])	;
	FOR [Wscript.Echo ] $ csrCONVERT.ctext ALL
REPLACE csrCONVERT.ctext WITH SPACE(5)								;
	+ ALLTRIM(csrCONVERT.ctext) + [>>] 								;
	FOR [Wscript.Echo ] $ csrCONVERT.ctext 							;
	AND [objItem] $ csrCONVERT.ctext ALL
REPLACE csrCONVERT.ctext WITH CHRTRAN(csrCONVERT.ctext,["],[]) 		;
	FOR [Wscript.Echo ] $ csrCONVERT.ctext ALL
REPLACE csrCONVERT.ctext WITH STRTRAN(csrCONVERT.ctext,[Wscript.Echo ],[\]) ALL
REPLACE csrCONVERT.ctext WITH CHRTRAN(csrCONVERT.ctext,[&],[+]) ALL

DELETE FOR LEFT(ALLTRIM(csrCONVERT.ctext),2) = [If] ALL
DELETE FOR LEFT(ALLTRIM(csrCONVERT.ctext),4) = [Else] ALL
DELETE FOR [Join(] $ ALLTRIM(csrCONVERT.ctext) ALL
DELETE FOR LEFT(ALLTRIM(csrCONVERT.ctext),3) = [End] ALL

LOCATE FOR [For Each] $ csrCONVERT.ctext
INSERT BLANK BEFORE
REPLACE csrCONVERT.ctext WITH [\\WMI class data report on <<DATE()>> at <<TIME()>>]
INSERT BLANK BEFORE
REPLACE csrCONVERT.ctext WITH [SET TEXTMERGE ON TO test.txt NOSHOW]
GO BOTTOM
INSERT INTO csrCONVERT VALUES ([SET TEXTMERGE OFF])
INSERT INTO csrCONVERT VALUES ([SET TEXTMERGE TO])

lcProgram = []
SCAN
	lcProgram = lcProgram  			;
		+ RTRIM(csrCONVERT.ctext) 	;
		+ CHR(13)
ENDSCAN

TRY
	EXECSCRIPT(lcProgram)
	lcString = FILETOSTR([test.txt])
	lcString = STRTRAN(lcString,[.NULL.],[])
	lcString = STRTRAN(lcString,[.T.],[Yes])	
	lcString = STRTRAN(lcString,[.F.],[No])
	lnOccurs = OCCURS(REPLICATE([-],35),lcString) / 2
	lcString = lcString 	        ;
		+ CHR(13)		;
		+ CHR(13)		;
		+ TRANSFORM(lnOccurs) 	;
		+ [ instances found]
	STRTOFILE(lcString,[test.txt])
	MODIFY FILE test.txt
CATCH	
	MESSAGEBOX([There is a error with the script])
	STRTOFILE(lcProgram,[test.txt])
	MODIFY FILE test.txt
ENDTRY[/color]
If you need to use the code in a VFP app, save the variable [color blue]lcProgram[/color] to a .prg.

Suggestions for improvements to this FAQ always welcome [smile]

FAQ184-2483 - answering getting answered.​
Chris [pc2]
[link http://www.pdfcommander.net]PDFcommander[sup]tm[/sup].net[/link]
[link http://www.pdfcommander.net]PDFcommander[sup]tm[/sup].com[/link]

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top