WMI Classes
Win32_Process, Win32_Service, AntiVirusProduct, Win32_Startup ...
WMI objects are queried using a
SQL like language:
WMI Query Language (WQL).
Most classes are implemented as
providers. Such providers are either
COM dlls or
kernel drivers.
Hierarchical Namespace
WMI classes are hierarchically organized into namespaces.
Some namespaces (such as
root\default
,
root\cimv2
,
root\subscription…
) are created when the system
boots up.
The namespaces in a given namespace (for example root
) can be shown like so:
PS C:\> get-cimInstance -namespace root -class __namespace | sort name
Name PSComputerName
---- --------------
CIMV2
Cli
DEFAULT
directory
Hardware
Interop
Microsoft
msdtc
PEH
RSOP
SECURITY
SecurityCenter
SecurityCenter2
ServiceModel
StandardCimv2
subscription
WMI
Similarly, the following command shows the namespaces under ROOT\CIMV2
(which happens to be he default namespace):
PS C:\> get-cimInstance -namespace root\cimv2 -class __namespace | sort name
The classes in given namespace can be showns like so:
PS C:\> get-cimClass -namespace root\cimv2
An instance of a particular class can then be obtained like so (note that -namespace root\cimv2
is not necessary here as it is the default namespace anyway):
PS C:\> get-cimInstance -namespace root\cimv2 -class win32_process
Recursively traversing namespaces
A
PowerShell script (that I found
here) can be used to show the namespaces:
function get-wmiNamespace {
param($namespace='ROOT')
get-wmiObject -namespace $namespace -class __NAMESPACE | forEach-object { (
$ns='{0}\{1}' -f $_.__NAMESPACE, $_.name)
get-wminamespace -namespace $ns
}
}
# $wmiClasses=
get-wminamespace | forEach-object {
$namespace=$_
get-wmiObject -namespace $namespace -list | forEach-object {
$_.path.path
}
} | sort-object -unique
VBScript
The following simple script demonstrates how WMI can be queried with
VBScript:
option explicit
dim winos
set winos = getObject("winmgmts:").instancesOf("Win32_OperatingSystem")
dim rec
for each rec in winos ' {
wScript.echo "Caption: " & rec.caption
wScript.echo "Manufacturer: " & rec.manufacturer
wScript.echo "Build Type: " & rec.BuildType
WScript.echo "Version: " & rec.Version
WScript.echo "Locale: " & rec.Locale
WScript.echo "Windows Directory: " & rec.windowsDirectory
WScript.echo "Total memory: " & round(rec.totalVisibleMemorySize/1024/1024) & " MB"
WScript.echo "Serial Number: " & rec.SerialNumber
Wscript.echo ""
next ' }
C:\users\rene> cscript query-Win32.vbs