Welcome, Sit down, and make yourself comfortable.
Using WMI
Published on April 5, 2007 By RomanDA In DesktopX Tutorials
Step-by-Step Tutorials

#5 - Using WMI

A series by RomanDA

Listing of other DX Tutorials: Click here
Today's Lesson: "Using WMI" Windows Management Instrumentation

In this lesson we will cover how to use Microsoft's WMI to find out some basic info about our windows computer.
In order to use these Tutorials you will need to purchase DesktopX for $14.95 from Stardock.
Lets get started.

STEP 1 - Create a text object.


I'm not going to go back over the first 4 tutorials, if you haven't read them, and gone thru them, please go back and do that first.

Create a TEXT objet, make the default text something like "TEST" it doesn't matter, make it the size you want (big enough to read on your screen).

 

We are simply going to be using this text object to display the info we gather.  Again, this is just to show you HOW to use WMI to retrieve info from your computer, not how to make a pretty widget.  We can do that later. 

 

Don't worry about adding scripts yet, we will do that in a later step.

STEP 2 - WMI Basic connection


WMI is used to pull information from your system, things like Computer name, User name, What drives are attached to your system.  We are going to pull this information from your computer using a simple script in DX.

Open your object, click on NEW for the script.
We will be adding some info to the "Sub Object_OnScriptEnter"
 

Vbscript  Code
  Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set compinfo = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
 
The DIM is used to Declare variables and allocates storage space.
The first SET command is used to make the connection to the local computer.  I could get all technical on this part, but if you really want to know all the details on this connect string visit this site.
The 2nd SET command is used to pull the information you want from the computer.  In this example we are wanting to get the user name, domain name, and computer name.  This works like a SQL Query string, there are a ton of options for this as well.  Again, this is a BASIC tutorial, so if you want more details, look the commands up.

Lets break this line apart:
Set CompInfo Set the variable CompInfo to the info we pull from WMI
objWMIService objWMIService was setup in the previous SET command to
point to the WMI service.
.ExecQuery ( Execute a query into the WMI database
"Select * we want to "SELECT" * - EVERYTHING
we could have picked just 1 item if we only wanted say the Username, etc.
from Win32_ComputerSystem") The data we want is coming from the Win32_ComputerSystem part of WMI
There are DOZENS of these tables in WMI.

HERE is a great place to learn all about WMI
 Lets look at what information we can pull from this one table:
I'm not going to go through ALL of this, I just want you to see what is available from this 1 table.
 
WMI Information
  class Win32_ComputerSystem : CIM_UnitaryComputerSystem
{
  uint16 AdminPasswordStatus;
  boolean AutomaticManagedPagefile;
  boolean AutomaticResetBootOption;
  boolean AutomaticResetCapability;
  uint16 BootOptionOnLimit;
  uint16 BootOptionOnWatchDog;
  boolean BootROMSupported;
  string BootupState;
  string Caption;
  uint16 ChassisBootupState;
  string CreationClassName;
   sint16 CurrentTimeZone;
   boolean DaylightInEffect;
   string Description;
   string DNSHostName;
   string Domain;
   uint16 DomainRole;
   boolean EnableDaylightSavingsTime;
   uint16 FrontPanelResetStatus;
   boolean InfraredSupported;
   string InitialLoadInfo;
   datetime InstallDate;
   uint16 KeyboardPasswordStatus;
   string LastLoadInfo;
   string Manufacturer;
   string Model;
   string Name;
   string NameFormat;
   boolean NetworkServerModeEnabled;
   uint32 NumberOfLogicalProcessors;
   uint32 NumberOfProcessors;
   uint8 OEMLogoBitmap[];
   string OEMStringArray[];
   boolean PartOfDomain;
   sint64 PauseAfterReset;
   uint16 PCSystemType;
   uint16 PowerManagementCapabilities[];
   boolean PowerManagementSupported;
   uint16 PowerOnPasswordStatus;
   uint16 PowerState;
   uint16 PowerSupplyState;
   string PrimaryOwnerContact;
   string PrimaryOwnerName;
   uint16 ResetCapability;
   sint16 ResetCount;
   sint16 ResetLimit;
   string Roles[];
   string Status;
   string SupportContactDescription[];
   uint16 SystemStartupDelay;
   string SystemStartupOptions[];
   uint8 SystemStartupSetting;
   string SystemType;
   uint16 ThermalState;
   uint64 TotalPhysicalMemory;
   string UserName;
   uint16 WakeUpType;
   string Workgroup;
  };

What we want from this table is:

  • UserName  - User Name
  • TotalPhysicalMemory - Total Memory
  • Domain - Domain name (If there is one)
  • Name - Computer Name

STEP 3 - Pulling info from WMI


So, we know how to make the connection to WMI, and we know what table we want.
Lets get the USERNAME that is logged into the current computer.
 

Vbscript  Code
  For Each objComputer In CompInfo
  PCName = objComputer.Name
  Next
  object.text = "ComputerName: " & PCName

Let's look over this one too.

I'm not going to break everything apart, but the basics are this:
the FOR EACH is used to pull 1 item at a time from the COMPINFO we set before, and store it in the objComputer variable. 
Then we assigning the variable PCName to the .NAME field from the table.
Then we take and set the TEXT of the object to "ComputerName: " & PCName

SAVE & APPLY this now, and see what happens.
What you SHOULD see is your text object showing something like:
ComputerName: MyPcsName

Not to hard was it?  Lets add a little more.
 

STEP 4 - Adding more info


Open the object back up, EDIT the script.  Lets add a little more info.
 

Vbscript  Code
  For Each objComputer In CompInfo
  PCName = objComputer.Name
  PCDomain = objComputer.Domain
  UserName = objComputer.UserName
Next
  object.text = "ComputerName: " & PCName & vbnewline
  object.text = object.text & "Domain: " & PCDomain & vbnewline
  object.text = object.text & "UserName: " & UserName & vbnewline

We added the Domain name (if there is one, you may not have one), and the Username.
Notice the USERNAME has the domain name as part of it? (if you have a domain, otherwise you wont see this).
We want to pull JUST the user name out from that. So lets add a simple IF statement to the script.

Vbscript  Code
  If len(PCName) > 1 Then
  t = len(PCName)+2
  UserName = mid(UserName,t)
End If

Insert this script right below the UserName = objComputer.UserName line.
This simply looks at the PCName to see if its LENgth is over 1 character
Then it gets the length of that name + 2 spaces (the \ and then the first letter of the name)
Then it sets UserName to the MIDdle of the the string UserName starting at the T postion.

Don't worry I will put the entire script at the bottom in one place, so you can copy/paste it if you want.

STEP 5 - What Drives do we have?


We are going to add a little more to this script.  One of the things people have asked to see is how to determine what drives, drive letters, etc. are on your pc.

 

The code we are going to add looks a lot like what we used above:
 

Vbscript  Code
  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set DriveInfo = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
object.Text = object.Text & vbnewline & "DeviceID | DriveType | Description "
object.Text = object.Text & " | VolumeName | FreeSpace | FileSystem"
For Each objDrive In DriveInfo
  object.Text = object.Text & vbnewline & objDrive.DeviceID & " | "
  object.Text = object.Text & objDrive.DriveType & " | " & objDrive.Description
  object.Text = object.Text & " | " & objDrive.VolumeName & " | "
  object.Text = object.Text & objDrive.FreeSpace & " | " & objDrive.FileSystem
Next

We are pulling a lot of info from the WIN32_LogicalDisk table.  add the code, see what shows up.
You should see something like:

DeviceID | DriveType | Description | VolumeName | FreeSpace | FileSystem
C: | 3 | Local Fixed Disk | Boot-2006b | 30640107520 | NTFS
D: | 3 | Local Fixed Disk | Storage-2006b | 14589018112 | NTFS
E: | 5 | CD-ROM Disc | 20030803_1304 | 0 | CDFS
F: | 5 | CD-ROM Disc | | |

STEP 5 - Here's The code:


Promised to post all the code in one place, here it is:

Vbscript  Code
  'Called when the script is executed
Sub Object_OnScriptEnter
 Dim objWMIService
 Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 Set CompInfo = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
 For Each objComputer In CompInfo
  PCName = objComputer.Name
  PCDomain = objComputer.Domain
  UserName = objComputer.UserName
  If len(PCName) > 1 Then
   t = len(PCName)+2
   UserName = mid(UserName,t)
  End If
 Next
 object.text = "ComputerName: " & PCName & vbnewline
 object.text = object.text & "Domain: " & PCDomain & vbnewline
 object.text = object.text & "UserName: " & UserName & vbnewline

 Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 Set DriveInfo = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
 object.Text = object.Text & vbnewline & "DeviceID | DriveType | Description "
 object.Text = object.Text & " | VolumeName | FreeSpace | FileSystem"
 For Each objDrive In DriveInfo
  object.Text = object.Text & vbnewline & objDrive.DeviceID & " | "
  object.Text = object.Text & objDrive.DriveType & " | " & objDrive.Description
  object.Text = object.Text & " | " & objDrive.VolumeName & " | "
  object.Text = object.Text & objDrive.FreeSpace & " | " & objDrive.FileSystem
 Next
End Sub

CONCLUSION


There are a lot of things that can be done with this info.  Things like making a Drive monitor, or a simple piece of text with your username etc on it.
WMI is EXTREMELY powerful, you can pull things from other computers on your network, A simple GOOGLE search will show you TONS of places with lots of script examples. 

Check back as I add new Step-By-Step Tutorials on how to make this a link to a folder, web-site, or just about anything you want!

I hope you have enjoyed this step into DX, and look forward to the next installment.

Enjoy,
RomanDA
http://romanda.wincustomize.com

Comments
on Apr 05, 2007
Nice work as always.

 
on Apr 05, 2007
Nicely done. Keep up the great work.
on Apr 05, 2007
More info....want more info....^^

You're really starting to impress me with all these complex scripts. I was pretty much satisfied with showing/hiding object, setting its state, but now I want to expand my knowledge.

Way to inspire people, RomanDA
on Apr 05, 2007
Wow!  Great idea for a tutorial.  You rock!

 
on Apr 05, 2007
Wow! Great idea for a tutorial. You rock!


OK OK OK OK OK.. shut up.. I get it.. yes YOU had a GREAT IDEA.. hell anyone can hit the ball ONCE out of 1000000 tries!
on Apr 05, 2007
"WMI is EXTREMELY powerful, you can pull things from other computers on your network"

Wow, I never knew this.

These are great tutorials. They are helping me a lot. Thanks for taking the time to do these.
on Apr 05, 2007
WMI is a really amazing tool, google search WMI and things your looking for, and you will see tons of results.
on Apr 08, 2007
I was always timid about this WMI stuff and pulling info from your computer but you made it look so simple. Now I'm gonna give it a shot. Thanks for the tut!


BTW: You're a JourneyMan now! And not for nothing either. You're work is a great community service. Thank you.      
on Apr 09, 2007
BTW: You're a JourneyMan now!


I think that happened about 4-5 months ago.. maybe I'm losing it.. LOL
on Apr 12, 2007
Really? Maybe I'm losing it!   Congrats, all the same.
on May 05, 2007
Excellent tutorials, buddy! Just a little note to say that you forgot END SUB at the end of the final code, but its not a criticism! Keep up the excellent work!
on May 05, 2007
forgot END SUB at the end of the final code,


Good catch, fixed. Thanks!
on Jan 31, 2009
Also forgot to name Info to CompInfo in first part of tuorial, that one had me banging my head on the keyboard You have Info in the code, but you call the variable Compinfo in your tuorial, and also use CompInfo later in the code. Good tutorials though
on Feb 28, 2009
Also forgot to name Info to CompInfo in first part of tuorial, that one had me banging my head on the keyboard You have Info in the code, but you call the variable Compinfo in your tuorial, and also use CompInfo later in the code. Good tutorials though
Yeah, I should've read these comments before banging my own head! I was about to report the same thing. Still a well written intro to WMI.
on Feb 28, 2009

Nyctea
Also forgot to name Info to CompInfo in first part of tuorial, that one had me banging my head on the keyboard You have Info in the code, but you call the variable Compinfo in your tuorial, and also use CompInfo later in the code. Good tutorials though


Yeah, I should've read these comments before banging my own head! I was about to report the same thing.

Still a well written intro to WMI.

 

Sorry.. fixed now