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. |
WMIWindows 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 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! ReferencesPlease 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 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 |
'Programming > Visual Basic' 카테고리의 다른 글
실행시킨 프로그램 끝날때까지(종료) 기다렸다 다음 코드 계속 실행하기 (0) | 2008.06.30 |
---|---|
VB Application XP 스타일 적용하기 (0) | 2008.05.14 |
VisualBasic 관련 사이트 모음. (0) | 2008.03.17 |
VB를 이용한 ActiveX 제작 (0) | 2008.02.21 |
<VB.NET>TEXT 파일 읽기, 쓰기 (1) | 2008.02.12 |