• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

About WMI

Started by José Roca, August 29, 2011, 04:06:49 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
Originally released in 1998 as an add-on component with Windows NT 4.0 Service Pack 4, WMI (Windows Management Instrumentation) is the core management-enabling technology built into Windows 2000, Windows XP, and the Windows Server 2003 family of operating systems. Based on industry standards overseen by the Distributed Management Task Force (DMTF), WMI is the instrumentation and plumbing through which all—well, almost all—Windows resources can be accessed, configured, managed, and monitored.

With WMI you can manage computers both locally and remotely over a network. It uses databases formatted according to the Common Information Model (CIM).

Windows XP Professional comes with WBEMTest a general-purpose GUI tool for viewing and modifying WMI classes, instances, and methods during the development of Windows Management Instrumentation (WMI) providers and WMI applications. You can also use WBEMTest to troubleshoot WMI and programs that depend on WMI.

The executable file for WBEMTest, Wbemtest.exe, is installed in the WBEM directory of your Windows system directory. On Windows XP systems, this is the %WINDIR%\system32\Wbem folder.


WBEMTest comes with good documentation. Click the Help button to access to it.

Another tool available is the Windows Management Instrumentation command-line (WMIC), that provides you with a simple command-line interface to Windows Management Instrumentation (WMI), so you can take advantage of WMI to manage computers running the Windows Server 2003 family of operating systems. WMIC interoperates with existing shells and utility commands, and can be easily extended by scripts or other administration-oriented applications.

To run WMIC in interactive mode click Start, Run, type WMIC, and then press ENTER. You can also use WMIC in non-interactive mode, which is useful if you use WMIC for a batch procedure, or if you only need to execute one WMIC command.

WMIC Documentation: Windows Management Instrumentation Command-line
WMI SDK Documentation: Windows Management Instrumentation
Tutorial: Windows Management Instrumentation Tutorial

For further reading:

WMI Scripting Primer: Part 1
WMI Scripting Primer: Part 2
WMI Scripting Primer: Part 3

WMI Administrative Tools (WMITools.exe) (Microsoft Download Center): WMI Tools include: WMI CIM Studio: view and edit classes, properties, qualifiers, and instances in a CIM repository; run selected methods; generate and compile MOF files. WMI Object Browser: view objects, edit property values and qualifiers, and run methods.


José Roca

 
One thing to be careful of when using the Count property is that WMI does not keep a running tally of the number of items in a collection. If you request Count for a collection, WMI cannot instantly respond with a number; instead, it must literally count the items, enumerating the entire collection. For a collection that has relatively few items, such as services, this enumeration likely takes less than a second. Counting the number of events in an event log collection, however, can take considerably longer.

José Roca

 
You can use the methods and properties of the SWbemObject object to represent one Windows Management Instrumentation (WMI) class definition or object instance.

This object supports two types of properties and methods. Those defined in this section are generic properties and methods that apply to all WMI objects. In addition, this object exposes the properties and methods of the underlying object as dynamic automation properties and methods of SWbemObject. The names and types of these properties and methods depend on the underlying WMI object.

The example below tells you how much free disk space is available on drive C of your computer.

Note that we are declaring oObject AS DISPATCH instead of oObject AS SWbemObject or pObject AS ISWbemObject to be able to use late binding. We need to use late binding because the exposed properties are dynamic.


' ########################################################################################
' The example below tells you how much free disk space is available on drive C of your computer.
' ########################################################################################

#COMPILE EXE
#DIM ALL
#INCLUDE "wmi.inc"
#INCLUDE "AfxVarToStr.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL hr AS LONG                                  ' // HRESULT
   LOCAL pService AS ISWbemServices                  ' // Services object
   LOCAL bstrObjectPath AS WSTRING                   ' // Object path
   LOCAL oObject AS DISPATCH                         ' // SWbemObject object
   LOCAL vRes AS VARIANT                             ' // General purpose variant

   ' // Connect to WMI using a moniker
   pService = WmiGetObject("winmgmts:\\.\root\cimv2")
   IF ISNOTHING(pService) THEN EXIT FUNCTION

   ' // Retrieve an instance of the object
   bstrObjectPath = "Win32_LogicalDisk.DeviceID='c:'"
   oObject = pService.Get(bstrObjectPath)
   IF ISNOTHING(oObject) THEN EXIT FUNCTION

   ' // Call the dynamic property(ies)
   OBJECT GET oObject.FreeSpace TO vRes
   ? AfxVarToStr(vRes)

   WAITKEY$

END FUNCTION
' ========================================================================================