Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.
QUERY FOR DATA FROM A WMI CLASS

If you need to recover data from a WMI class, the .prg posted in this FAQ will take the code generated by Microsoft's WMI Code Creator 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.

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

If you need to use the code in a VFP app, save the variable lcProgram to a .prg. Suggestions for improvements to this FAQ always welcome.

ABOUT THE AUTHOR: CHRIS CHAMBERLAIN

Chris Chamberlain Chris has worked with FoxPro since the early 1990s. He currently works as an independent VFP consultant based in Gloucestershire, but also collaborates with others on a variety of different Windows technologies. He is the lead developer of PDFcommander™, http://www.pdfcommander.net, and has other similar projects in progress.’

FEEDBACK


Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: