Programming/Visual Basic

WMI - VB 6.0 Windows Management Instrumentation

bcheul 2009. 2. 24. 07:25

In this article, we will see a brief introduction about Windows Management Instrumentation (WMI) and the key areas where it can be used to manage our system efficiently.


WMI

 

Windows Management Instrumentation, in short is a repository of Windows System Information which can be used to administer and also to list intricacies of Windows System in an efficient way. There are lots of advantages in using WMI, just to list a few of them,

 

  • WMI can be used to manage local and remote computers.

 

  • Execute a Process in a local/remote computer at specific time or specific day, these routine activities are very much important for a System Maintenance guy who works for an any IT based company. They can very well use WMI to accomplish such tasks in an easy way.

 

  • Reboot local/remote Computer and getting list of software’s installed in local/remote computer. In current scenario, many of the IT based companies monitor individual systems for illegal downloads or for licensed software’s to avoid pirated versions. WMI plays an important role in developing such scripts.

 

WMI is built based on several system classes like,

 

Win32_Process - provides information about processes currently running on a computer

Win32_LogicalDisk- provides information about logical disks installed on a computer

Win32_OperatingSystem - provides information about the installed OS

 

Let us see the power of WMI using some code samples,

 

Open a VB Standard EXE Project and place a listbox control in Form and paste the below code,

 

Private Sub Form_Load()

 

dim colProcessList

dim objprocess

 

Set colProcessList = GetObject("Winmgmts:").ExecQuery("Select * from Win32_Process ")

 

For Each objprocess In colProcessList

    List1.AddItem "Process Name=" & objprocess.Name

    List1.AddItem "Caption=" & objprocess.Caption

Next

 

Set colProcessList = Nothing

set objprocess = nothing

End Sub

 

In the above code, we are trying to fetch the details about all process running in the current domain from Win32_Process class using WMI framework. objprocess has lot of Methods and Properties and to drill down more about it, we need to go for a WMI Tool which does this job for us.

 

CIM Studio is a tool that enables you to browse WMI Classes on Windows 2000 and later platforms. For information on this tool go to http://www.microsoft.com and search for the keyword “WMI tools.”

 

The above code can be changed to verify whether Notepad.exe is running in your machine, and if found "Terminate" the Notepad Application,

 

Private Sub Form_Load()

 

strprocess = "notepad.exe"

 

Set colProcessList = GetObject("Winmgmts:").ExecQuery("Select * from Win32_Process where name = '" & strprocess & "'")

 

For Each objprocess In colProcessList

'Terminate method kills the notepad application

objprocess.Terminate

Next

 

Set colProcessList = Nothing

End Sub

 

You can also use Windows API function to terminate a running process, but the code might look complicated and you have include lot of constants and API functions to your code in order to Terminate a running process. WMI helps us to do this in a better way.

 

Using WMI you can also connect to a remote computer using the below command,

 

Set objWMIService = GetObject("winmgmts:\\DomainName\TargetComputer”)

 

Please note that you should have appropriate permissions on the remote machine. If you have these permissions, you can connect to the remote machine without specifying user credentials. WMI will connect using the user credentials you logged on with.

 

Even if you don't specify user credentials, you can connect to a remote computer using the short connection syntax known as a moniker string. For more information on how to build a Monkier String, refer http://msdn.microsoft.com/library/default.asp.

 

 

Let us see another example which lists uninstalled application names from Registry,

 

Open a VB Standard EXE Project and paste the below code in Form_load() event,

 

'connect to current machine registry

strHost = "."

Const HKLM = &H80000002

'connect to current machine registry

Set objReg = GetObject("winmgmts://" & strHost & "/root/default:StdRegProv")

Const strRoot = "Software\Microsoft\Windows\CurrentVersion\Uninstall\"

'check the registry for this particular Uninstall path

'and return the values inside Uninstall to an array - arrsubkeys

objReg.EnumKey HKLM, strRoot, arrSubKeys

 

'Enumerate the array and display the Name of the Application which was uninstalled

For Each strSubk In arrSubKeys

    intRet = objReg.GetStringValue(HKLM, strRoot & strSubK, _

        "DisplayName", strValue)

    If (strValue <> "") And (intRet = 0) Then

        Debug.Print strValue

    End If

Next

 

 

There are lot more which can be done using WMI Scripts, explore WMI and sooner you can become an expert in Administering Windows!

 

 

References

 

Please refer below links to know more about WMI,

 

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_reference.asp

 

Please find some sample Scripts for WMI at
http://go.microsoft.com/fwlink/?LinkId=24771

 

You can also post your queries about WMI applications to Microsoft newsgroups,

 

Microsoft.public.win32.programmer.wmi

 

Microsoft.public.windowsxp.wmi

 

Microsoft.public.windows.server.scripting