2. About Nick Schweitzer Senior Consultant for SpiderLogic in Milwaukee Milwaukee Area Consultant for 12+ years B.S. in Computer Engineering from MSOE Around the Internets: thecodingmonkey.net (Geek/Technical) nickschweitzer.net (Life/Politics) flickr.com/photos/schweitn (Amateur Photography) twitter.com/NickSchweitzer (Real Time) facebook.com/NickSchweitzer (Meta)
3. Agenda What is WMI? WMI Architecture Querying System State with WMI Instrumenting .NET Applications with WMI WMI Tools Cool Uses for WMI WMI Related Projects and Resources
4. Warning This is Not Bleeding Edge Technology You Could Have Been Doing this For the Last 8 Years… If You Knew It Was There.
5. What is WMI? Windows Management Instrumentation Microsoft’s Implementation of WBEM (Web-Based Enterprise Management) Allows Machine and Software State to be Exposed to Local and Remote Queries for Enhanced Enterprise Management
6. Why Instrument for WMI? Provides Many Methods to Query Application State .NET Query (System.Management namespace) Powershell MOM/SCOM Monitoring Visual Studio Analyzer Inter-process Signaling & Communication Useful for Monitoring and Debugging Services w/o User Interfaces Watchdog Processes and Redundant/Failover Services
7.
8. Common Information Model (CIM) Language Independent Model for Representing Enterprise Objects Supports Inheritance & Object Relationships Supports Schemas for Objects CIM_* (Core/Common Classes) Win32_* (Windows Implementation Specific Classes) Can Create User Defined Schemas
9. WMI Namespaces Independent of .NET Namespaces Best Practice is to create a Root Namespace for Company and Sub-Namespaces for applications or services User Access Security can be set at a Namespace level on each machine. Win32_* Objects in ROOTIMV2 Used for Querying Machine & Operating System state
10. WMI Query Language (WQL) SQL Subset Used to Query WMI Repository Get Name of All Running Services SELECT DisplayName FROM Win32_Service WHERE State = 'Running' Get Local Computer Name SELECT Name FROM Win32_ComputerSystem Get List of All Local Groups SELECT * FROM Win32_Group WHERE LocalAccount = True
11. Subscribing to Events with WQL Can Be Notified of the Following: Instance Creation and Deletion Property Changes Custom Events Example – New Process Notification SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process' WITHIN Keyword Defines Polling Frequency in Seconds
13. .NET Instrumentation with WMI Attribute Based Two Sets of Attributes in One Assembly System.Management.Instrumentation Both sets can be used together in one application. Both sets cannot be used together in one class. Requires Assembly Registration
14. Original WMI Attributing (v1) Properties Instrumented by Default Resulting WMI objects can only be used for Query Can Only Instrument Properties that are Value Types Cannot Expose Methods Available for Use in .NET 1.0 and later
15. Custom WMI Classes (v1) InstrumentationClass Marks a class as being Instrumented All properties are Instrumented by Default Can also derive from Instance class instead IgnoreMember Marks a property as being not Instrumented Required for properties that cannot be Instrumented Instrumentation.Publish() Makes Object Available to WMI Repository
16. Custom WMI Events Only Available in v1 InstrumentationClass InstrumentationType = Event Call Instrumentation.Fire()to Raise Event Derive class from BaseEvent Call Fire()method to Raise Event Events Can Only Reference WMI Primitives
17. WMI Registration (v1) Instrumented Attribute Assembly Level Attribute Specify Namespace DefaultManagementProjectInstaller Use InstallUtil.exe to register Namespace and CIM Schema
18. New WMI Attributing (v2) Properties Instrumented only if Attributed Can Create New .NET objects using WMI Invoke Constructors, Methods, Change Properties, etc. Can Instrument Properties that are Reference Types (if also Instrumented) Available for Use in .NET 3.5 and later.
19. Custom WMI Classes (v2) ManagementEntity Marks a Class as being instrumented (equivalent to InstrumentationClass in v1) ManagementKey Marks a Property as being a unique identifier ManagementProbe/ManagementConfiguration Marks a Property as being instrumented MangementBind Marks a Method/Constructor available to WMI for object creation ManagementTask Marks a Method available to WMI for invocation InstrumentationManager.Publish() Makes Object Available to WMI Repository
20. WMI Registration (v2) WmiConfiguration Assembly Level Attribute Specify Namespace HostingModel Use Decoupled for Application Instrumenation DefaultManagementInstaller Use InstallUtil.exe to register Namespace and CIM Schema
21. Runtime WMI Registration Registration Possible w/o InstallUtil.exe Registering All Types in an Assembly Instrumentation.RegisterAssembly (v1) InstrumentationManager.RegisterAssembly (v2) InstrumenationManager.UnregisterAssembly (v2) Registering Individual Classes InstrumentationManager.RegisterType(v2) InstrumentationManager.UnregisterType(v2)
22. Which Attribute System? If your application does not require v2 WMI Attributes, then don’t use them. The object management is not worth the hassle.
24. WMI in WCF Used to Turn On WCF Diagnostics at Runtime Must Enable WMI in Configuration File:<system.serviceModel> … <diagnostics wmiProviderEnabled="true" /> … </system.serviceModel> Published in rooterviceModel AppDomainInfo Object LogMalformedMessages : Boolean LogMessagesAtServiceLevel : Boolean LogMessagesAtTransportLevel : Boolean
25. WMI in WCF Continued Using Powershell to Get WCF Diagnostic Settings Get Diagnostic Settings:get-wmiobject -class "AppDomainInfo" -namespace "rootervicemodel" -computername "." Changes Made at Runtime Not Stored in Configuration File – Only Valid During Current Run
26. WMI “Productivity” Tools Windows Management Instrumentation Tester Used to Browse and Query WMI Objects and Schemas wbemtest.exe WMI Code Creator Utility to Quickly Generate WQL Code in Multiple Languages Management Strongly Typed Class Generator Generates Strongly Types Classes for Querying and Accessing WMI objects mgmtclassgen.exe (Windows SDK) WMI CIM Studio ActiveX powered tool used to browse WMI objects on local machine.
28. Custom MMC Snap-Ins Requires MMC 3.0 (Separate Install for Win XP) Run mmcperf.exe Before Installing a New Snap-In Can Hook Into Computer Management if you know the right GUID’s Snap-In DLL’s Registered using InstallUtil.exe Relatively Easy to Determine Target Computer to Query Provides Framework and Base Classes for Creating Uniform Looking MMC Snap-In UI
30. Presentation Resources Presentation Slides and Code Samples www.thecodingmonkey.net Microsoft Downloads WMI CIM Studio WMI Code Creator MMC Extensible Node GUID’s Computer Management GUID List Server Manager GUID List
31. Other Online Resources Windows Management Infrastructure Blog http://blogs.msdn.com/wmi/ Linq to WMI Project http://linq2wmi.codeplex.com Not Actively Maintained, and Somewhat Incomplete
32. Questions? Help Make My Presentations Better Please Fill Out an Evaluation Form! Opinions are Completely Anonymous (Unless You Don’t Want Them to Be) Thank You!