WMI?
• What Means It?
• Who Made It?
• What Makes It tick?
• Where Might I use it for?
Who Made It?
• Set of Standards created by DMTF
(Desktop Management Task Force Distributed Management Task Force)
What Means It?
• Set of Standards created by DMTF
(Desktop Management Task Force Distributed Management Task Force)
Consisting of:
DMI (Desktop Management Interface)
WBEM (Web-Based Entreprise Management)
CIM (Common Interface Model)
Bundled together known as Windows Meta Instrumentation.
What Makes It tick? (1)
Namespace(s)
Provider Provider
Class(es) Class(es) Class(es)
What Makes It tick? (2)
Namespace(s)
Provider Provider
Class(es) Class(es) Class(es)
WMI Layer
COM-API, SCRIPTING API
OS Layer
What Makes It tick? (3)
Namespace(s)
Provider Provider
Class(es) Class(es) Class(es)
WMI Layer
COM-API, SCRIPTING API
OS Layer
WMI Classes, Objects & Properties
COM-API, SCRIPTING-API, ...
OS Specific Binaries, Registry, WBEM MIF & MOF Files, ...
NT4 W2K W2K3 W2K8 XP Vista ...
Where Might I use it for?
A Quick & \"Uniform\"
Development Interface
Reporting Interface
Device Management Interface
Wow My…It even has a Wizard ;-)
\"Scriptomatic\"
• \"A completely new version of the famous Scriptomatic, the utility that
writes WMI scripts for you.
(And, in the process, teaches you the fundamental concepts behind
writing WMI scripts for yourself.)
• Unlike its predecessor, Scriptomatic 2.0 isn’t limited to writing just
VBScript scripts; instead, Scriptomatic 2.0 can write scripts in Perl,
Python, or JScript as well.
• In addition, Scriptomatic 2.0 gives you a host of new output formats to
use when running scripts, including saving data as plain-text, as a
stand-alone Web page, or even as XML. Scriptomatic 2.0 handles arrays,
it converts dates to a more readable format, and it works with all the
WMI classes on your computer; on top of all that, it also writes scripts
that can be run against multiple machines.“*
*Quote Microsoft Download Center
Download: http://www.microsoft.com/downloads/details.aspx?FamilyID=09dfc342-648b-4119-
b7eb-783b0f7d1178&DisplayLang=en
Example Perl WMI Query
1. use strict;
2. use Win32::OLE('in');
3. use constant wbemFlagReturnImmediately => 0x10;
4. use constant wbemFlagForwardOnly => 0x20;
5. my @computers = (\".\");
6. foreach my $computer (@computers) {
7. my $objWMI = Win32::OLE->GetObject(\"winmgmts:\\\\\\\\$computer\\\\root\\\\CIMV2\")
or die \"WMI connection failed.\\n\";
8. my $colItems = $objWMI->ExecQuery(\"SELECT * FROM Win32_NetworkAdapter\", \"WQL\",
9. wbemFlagReturnImmediately | wbemFlagForwardOnly);
10. foreach my $objItem (in $colItems) {
11. print (\"TimeOfLastReset: \"
12. .UTCDate($objItem->{TimeOfLastReset}).\"\\n\");
13. print \"TimeOfLastReset: $objItem->{TimeOfLastReset}\\n\";
14. print \"\\n\";
15. }
16. }
17. sub UTCDate(){
18. my $WmiDate = qr/(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})
19. (\\d{2})\\.(\\d{6})([\\+\\-])(.*)$
20. /ox;
21. $_[0] =~ s/$WmiDate/$1 $2 $3 $4h $5m $6s (UTC$8$9m)/;
22. return $_[0];
23. }
24. #TimeOfLastReset: 20071027003940.826905+120
25. #TimeOfLastReset: 2007 10 27 00h 39m 40s (UTC+120m)
Connecting Perl to WMI (1)
• Moniker
1. use Win32::OLE('in');
2. use strict;
3. $strComputer = '.';
4. $objWMI = Win32::OLE->GetObject('winmgmts:\\\\\\\\' . $strComputer .
'\\\\root\\\\cimv2');
Constructing a Moniker String: http://msdn2.microsoft.com/en-us/library/aa389292.aspx
Connecting Perl to WMI (2)
• SWBEM Locator
1. use Win32::OLE('in');
2. use strict;
3. my $objWMILocator = Win32::OLE->CreateObject(\"WbemScripting.SWbemLocator\");
4. $objWMILocator->Security_->{AuthenticationLevel} = 6;
5. my $objWMIComputer = $objWMILocator->ConnectServer($strComputer, \"root\\\\cimv2\",
$strLocalUser, $strLocalPasswd);
Connecting to WMI on a Remote Computer: http://msdn2.microsoft.com/en-us/library/aa389290.aspx
WQL (SQL for WMI)
AND NOT
ASSOCIATORS OF NULL
FALSE OR
FROM REFERENCES OF
GROUP Clause SELECT
HAVING TRUE
IS WHERE
ISA WITHIN
KEYSONLY __CLASS
LIKE
WQL (SQL for WMI): http://msdn2.microsoft.com/en-us/library/aa394606.aspx
WQL - ExecQuery
1. my $objWMI = Win32::OLE->GetObject(\"winmgmts:\\\\\\\\$computer\\\\root\\\\CIMV2\")
or die \"WMI connection failed.\\n\";
2. my $colItems = $objWMI->ExecQuery(\"SELECT * FROM Win32_NetworkAdapter“,
\"WQL\", wbemFlagReturnImmediately | wbemFlagForwardOnly);
3. foreach my $objItem (in $colItems) {
4. print ( \"TimeOfLastReset: \"
5. . UTCDate($objItem->{TimeOfLastReset}).\"\\n\");
6. print \"TimeOfLastReset: $objItem->{TimeOfLastReset} \\n\\n\";
7. }
Calling a Method: http://msdn2.microsoft.com/en-us/library/aa384832.aspx
WbemFlagEnum: http://msdn2.microsoft.com/en-us/library/Aa393980.aspx
Example Perl WQL ‘Associators Of’
1. use win32::ole;
2. use strict;
3. my $objWMI = Win32::OLE->GetObject('winmgmts:\\\\\\\\.\\\\root\\\\cimv2')
or die \"WMI connection failed.\\n\";
4. my $colNAs = $objWMI->ExecQuery( 'select * ' . ' from Win32_NetworkAdapter' );
5. foreach my $objNA( in $colNAs) {
6. my $colSubNAConfig =
7. $objWMI->ExecQuery( 'ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID=\\''
8. . $objNA->DeviceID . '\\'} '
9. . ' WHERE resultClass = '
10. . ' win32_NetworkAdapterConfiguration' );
11. foreach my $objNAConfig ( in $colSubNAConfig ) {
12. if ( $objNAConfig->DHCPEnabled == 1 ) {
13. my $intReturnCode = $objNAConfig->RenewDHCPLease();
14. if ( $intReturnCode == 0 ) {
15. print \"Renewed IP Configuration for \".$objNA->Name ;
16. }
17. elsif ( $intReturnCode == 1 ) {
18. print \"You must reboot to renew the IP Configuration for \"
19. .$objNA->Name ;
20. }
21. }
22. }
23. }
WMI UTC Date/Time (1)
Based on the CIM datetime/interval format
Formatting: yyyymmddHHMMSS.mmmmmmsUUU or yyyy-mm-dd HH:MM:SS:mmm
1. foreach my $objItem (in $colItems) {
2. print (\"TimeOfLastReset: \"
.UTCDate($objItem->{TimeOfLastReset})
3.
4. .\"\\n\");
5. print \"TimeOfLastReset: $objItem->{TimeOfLastReset}\\n\";
6. print \"\\n\";
7. }
8. }
9. #TimeOfLastReset: 20071027003940.826905+120
10. #TimeOfLastReset: 2007 10 27 00h 39m 40s (UTC+120m)
WMI UTC Date/Time (2)
Based on the CIM datetime/interval format
Formatting: yyyymmddHHMMSS.mmmmmmsUUU or yyyy-mm-dd HH:MM:SS:mmm
1. sub UTCDate(){
2. my $WMIDate = $_[0];
3. $WMIDate =~ s/[^0-9\\-\\+]//gis;
4. my $WmiDateRaw = qr/(\\d{4})(\\d{2})(\\d{2})
5. (\\d{2})(\\d{2})(\\d{2})
6. (\\d{6})([\\+\\-])(.*)$
7. /ox;
8. my $WmiDateFormatted = qr/(\\d{4})\\-?(\\d{2})\\-?(\\d{2})
9. (\\d{2})(\\d{2})(\\d{2})
10. (\\d{3})$
11. /ox;
12. if ($WMIDate =~ s/$WmiDateRaw/$1 $2 $3 $4h $5m $6s (UTC$8$9m)/){
13. return $WMIDate;
14. };
15. if ($WMIDate =~ s/$WmiDateFormatted/$1 $2 $3 $4h $5m $6s/){
16. return $WMIDate;
17. };
18. }
19. #TimeOfLastReset: 20071027003940.826905+120
20. #TimeOfLastReset: 2007 10 27 00h 39m 40s (UTC+120m)
WMI Practical: ‘Route Print’
1. use strict;
2. use Win32::OLE('in');
3. use constant wbemFlagReturnImmediately => 0x10;
4. use constant wbemFlagForwardOnly => 0x20;
5. my $objWMIService = Win32::OLE->GetObject(\"winmgmts:\\\\\\\\localhost\\\\root\\\\CIMV2\")
6. or die \"WMI connection failed.\\n\";
7. my $colItems = $objWMIService->ExecQuery(\"SELECT * FROM Win32_IP4RouteTable\",
\"WQL\",
wbemFlagReturnImmediately | wbemFlagForwardOnly);
8. if (Win32::GetLastError()){
9. # Do Error Handling.
10. };
11. print \"Destination\\t\".\"InterfaceIndex\\t\".\"Mask:\\t\".\"Metric1:\\t\".
\"Name:\\t\".\"NextHop:\\t\".\"Type:\\t\".\"Protocol:\\n\";
12. print ‘-’ x 78;
13. foreach my $objItem ( in $colItems) {
14. print \"$objItem->{Destination}\\t\";
15. print sprintf \"0x%x\", $objItem->{InterfaceIndex}.\"\\t\";
16. print \"$objItem->{Mask}\\t“.\"$objItem->{Metric1}\\t\";
17. print \"$objItem->{Name}\\t“.\"$objItem->{NextHop}\\t\";
18. print \"$objItem->{Type}\\t“.print \"$objItem->{Protocol}\\n\";
19. }
WMI Practical: ‘Refreshing’
• VBS (`cscript script.vbs`)
1. ' Get the performance counter instance for the System process
2. set Perf = GetObject(\"winmgmts:Win32_PerfFormattedData_PerfOS_Processor.name=‘0'\")
3. ' Display some properties in a loop
4. for I = 1 to 5
5. Wscript.Echo \"Perf PercentIdleTime = \" & Perf.PercentIdleTime
6. Wscript.Sleep 2000
7. ' Refresh the objects
8. Perf.Refresh_
9. next
C:\\>cscript Demo_Refresher.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Perf PercentIdleTime = 100
Perf PercentIdleTime =
Perf PercentIdleTime = 100
Perf PercentIdleTime = 96
Perf PercentIdleTime = 96
C:\\>
WMI Practical: ‘Refreshing’
• PLS (`cscript script.pls`)
1. use Win32::OLE qw(in);
2. use strict;
3.
4. my $Perf = Win32::OLE->GetObject(
\"winmgmts:Win32_PerfFormattedData_PerfOS_Processor.name=‘0'\");
5. for (my $i = 1;$i <= 5 ;$i++){
6. sleep 5;
7. $Perf->Refresh_();
8. print \"PercentIdleTime: $Perf->{PercentIdleTime}\\n\";
9. }
C:\\>cscript Demo_Refresher.pls
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Perf PercentIdleTime = 100
Perf PercentIdleTime =
Perf PercentIdleTime = 96
Perf PercentIdleTime = 93
Perf PercentIdleTime = 97
C:\\>
1 comments
Comments 1 - 1 of 1 previous next Post a comment