INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

WMI

Query for data from a WMI class by ChrisRChamberlain
Posted: 16 May 06 (Edited 18 May 06)

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.

CODE

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 smile

FAQ184-2483 - answering getting answered.
Chris pc2
PDFcommandertm.net
PDFcommandertm.com


Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close