The Project Reason
Anyone that has used the WMI Code Creator knows how fast you can get up and going. Since most of my programs that use WMI are Windows forms it was becoming a time consuming thing to create the code with the selected properties from the WMI Code Creator and then port that code to a Windows forms style application.
So I decided to see if I could build my own code creator for faster builds. When you have 15 properties is takes a lot of time, and to make sure you spell them correctly or you end up with a "Not Found" error for a property name that was misspelled.
This project is still a work in progress. There are a few known Issues.
If you start the program on a Windows Vista system with UAC Enabled and just click on the exe or click the run button in the IDE then it takes Approximately 60 seconds for the main form to load versus about 4 seconds If you right click and "Run as Administrator" from the Release folder.
If UAC is enabled you may need to Run The Code Creator as a Administrator, or it may error out. I have not yet added any Code to Auto Elevate.
(something keeps re-enabling my UAC, still looking into that problem)
Not All classes return Information.
The current version only works with the text box output. Still working on the ability to output for the data grid view and list box.
Now supports datagrid view, and listbox.
The Building of the Program
I first started out by looking at the WMI Code Creator wanting to keep some of the same feel of that program.
My first version had a checked list box, but you had to click twice for every property you wanted to select. So I later changed that to a regular list box. One left click does it.
Then I used a rich text box but you don’t get the instant right click copy ability so I changed that to a regular text box.(was having to use CTL+ C to do it while testing)
I borrowed the code from a previous project for filling the Name spaces and the Classes.
The code formatting was a matter of looking at a regular programs code and then looking to see how to display the code on output from the Code Creator. (lots of trial and error)
Another problem I ran into was loading the property’s into the list box. The first version would pop up a "Not Found " error, it took a bit to figure out it wasn’t finding the class if it wasn’t a CimV2 class, not the property name that usually pops up that error. So I went back to the MSDN site for the System. Management and was able to figure it out from there. (See the project code in the download for better understanding)
As I was building it I had to look at it as 2 different sets of code. The first for building the strings for the output and the second for how the output would need to display.
One of the problems I ran into was, where the output needed to have double quotes around it, a quick internet search led me in the right direction but didn’t give the exact answer.
The Answer was to,
strbuilder.AppendLine(ChrW(34) & "SELECT * FROM " & SCL.ToString & ChrW(34) & ")")
To get this in the output box:
"SELECT * FROM CIM_CDROMDrive")
Here is what the current version looks like.
Using the program
To use this program right click and run as admin (just to avoid some problems).
After the splash screen closes, the main form will open and the Namespace list will be filled.
Select a namespace, then the Classes list will fill. The number above tells how many classes or name spaces were found.(still need to work on that part)
Once you have selected a class, the list box will fill with the properties. Select the properties you want by left clicking on them(unless your buttons are reversed), if the property name is highlighted, then it is selected. You can left click again to unselect.(the select method in the list box properties page is multi simple)
Then Just click on the Create code button and the output text box will fill.
Create a project, add a button control and a Text box to your new form. Name you button and set the text for it.
Then on the textbox make it a multiline and size it to your form. Next set the textbox properties.
For me I ,
- Set Anchor to all 4 after setting the size of the textbox. (so the text box with grow or shrink as the form is resized)
- change font size to 10 For the higher resolution screens.
- Set scroll bars to both (Solves problems later when something doesn’t fit)
- Set Word wrap to false (Output gets confusing at times when it wraps to the next line)
If you want to use an on load event for your program then double click on the form to create it. I prefer to have mine at the top of the code.
Then click on your button control to get the handler for that.
Add some room above the "Public Class Form1" to add the Import Statements.
Then copy the Import Statements from the Code Creator to the area you just made above.
Then Add the References for the Import Statements. (Doing that now will eliminate the squiggly lines that would appear otherwise in the next step.)
Copy the code from the code creator to the button click handler.
Starting at: "Dim strbuilder As New StringBuilder"
MessageBox.Show("An error occurred while querying for WMI data:"& err.Message)
If your program (Class) uses the DTMF date time (Output Looks like this with out conversion , 20120426215915.359000-300 )then copy the function for converting it to a normal date/time. I am including a Program that shows how to use the function. Not all classes use that form of date, you may not need to add the function. (The TS app errored when I added it for that one, helps to know what the output should look like.)
As long as there are no errors, build and run your new program.
Tweak as desired : )
Again These are management programs if you are running Vista or above and get an initialization error then right click and run the program as Administrator.
Again, not all classes will return information, you may want to use one of the following application to see what gets returned before building a new application, and to help pick any properties you may want use. Some property names never return anything, so no point cluttering up your output with the extra property names. The selected class also may not return anything at all.
Programs to test out:
The WMI Code creator, Scriptomatic 2.0, or My ClassNFOMatic24.HTA that I will add as a separate download. My ClassNFOMatic24.HTA is a Highly modified version of the Scriptomatic 2.0 that will list all classes. I will also add in that download a registry file for adding the right click "Run as Admin" for HTA’s .
The sample applications took about 20 minuets or less to complete using the code creator.
Please let me know what you think about this. I will get the rest of it going as I find time.
Using double quotes in strings
Run AS For HTA
Scriptomatic 2.0 download
WMI Code Creator v1.0 Link to CNet Download.com (No Longer On Microsoft downloads)
Sample Files from my Sky drive
(File Names On my Sky Drive: ClassNFOMatic24.zip, GUI WMI exe file.zip, GUIWMI Project FIles.zip, SampleAppProjectFiles.zip, SampleApps-exes.zip
Latest project file names are:
GUI WMI Code Creator-184.108.40.206.zip (new EXE file only)
Converted the project Using VS 2012 Pro (Trial) (still VB.net) and Added to downloads.
Updated link for the Microsoft Wmi Code Creator.
Now points to Download.com was pointing at Microsoft downloads.
New version 220.127.116.11
Added require Administrator, uploaded files to Code Project.
New version 18.104.22.168
Added an If statement to the convert date function to handle the error when a null date is encountered. Now will return N/A for a null date.
In the Text box version only, implemented the code for working with Array list and for the date/time
So user doesn’t have to add that part ,If that type of property was selected. Works more like the scriptomatic now.
New file name listed above for the exe only. Now Available from the skydrive.
Had some formatting issues, I apologies to everyone for all of the updates.
Latest Version: 22.214.171.124 5/6/2012
Initial program version is : 126.96.36.199 Date: 5/5/2012