Mon, Dec 12 2005 11:32 AM Emile Bosch

Query your WMI with ease using WMILinq!

When I first played with Linq I didnt move away from my computer for two days, neglecting my friends and family, while gazing at Linqs tremendous power to take over the world. I was particulary interrested in its sexy feature to convert an Linq expression into an so called expression tree, as you can experience with DLinq. As you probably already know,  when you write an linq query the default compiler behaviour converts the query to an delegate. But this behaviour radically changes when you assign your query to an Expression<T> class. In this way the compiler doesn’t convert your query into an delegate but into an expression tree. It is then up to you on how to process this tree in something useful and come up with the right output. There is an excellent article on this behaviour from Ian Griffiths you can find its link below.
 
Making life easier with WmiLinq
 
To show the use and power of expression trees I wrote a basic WMILinq library which makes it possible to query directly against WMI store using an LINQ like syntax. Internally it works somewhat similar as Dlinq, it translates an LINQ query to an expression tree and feeds it to an Query Builder. This analyzes the expression tree and generates an WMI WQL compatible query, which is fed to the WMI library of .NET. When this query is executed the results are mapped back using reflection into entities. I kept it all a bit simple by only supporting the basic WHERE clauses, thus joins are not supported. And for clarity i havent included any query validation, optimization or whatsoever.
  
Its all pretty beta and purely intended as an expression tree sample and educational project. But it sure is interesting to see what is possible with expression trees with such a limited implementation. If microsoft isnt building the same thing right now i might continue developing it though.
 
Lets say we want to query some process information.  For instance, query the processes which have more than 20 kernel threads:
 
using (WmiContext context = new WmiContext(@"\\localhost"))
{               
   var query = from process in context.Source<Win32_Process>() where
          process.ThreadCount > 20 && process.Name.Contains(".exe") select process;
  
   foreach (Win32_Process process in query)
   {
     Console.WriteLine(process.Name + process.CreationDate);
   }
 
  //The above results in the following WQL Query:
  //SELECT * FROM Win32_Process WHERE ThreadCount > 20
}
 
Or start doing things which make totally no sense but to show off our cross linq languages.
For example: Query the desktops and  save the result in xml.
 
using (WmiContext context = new WmiContext(@"\\localhost"))
{        
   var doc = new XDocument(
           new XElement("Desktops", (from desktop in context.Source<Win32_Desktop>()
            where desktop.Name.Contains("service") select
                  new XElement("Desktop", new XAttribute("name", desktop.Name)))));
 
   //The above rusults in the folllowing WQL Query:
   //SELECT * FROM Win32_Desktop WHERE Name Like ‘%service%’      
}
 
As you can see its easy to query every piece of  information that is available in WMI. Hardware information, network information, event logs, performance counters, installed software and so on. I never had much field experience on WMI, and i am not sure how long WMI is going to last, but WMI seemed like an easy enough query language to create an sample for.
 
Create your prototype entities with WmiClassGen
 
As you can see in the examples, im querying the WMI store using the prototype entities like Win32_Process and Win32_Desktop , these are used for specifying the WHERE clause and for mapping the flat data back into entities. Which are somewhat similar as the table classes you use with Dlinq.  Al this neatness isnt much worth if you have to write the WMI entities yourself. Its not much fun, error prone and way too much work for us lazy developers. Therefor I made little tool which generates the WMI entity for you using codedom. The tool does the same thing for WMI as SQLMetal does for databases. Creating an prototype class is as  easy like this.
 
> WmiClassGen.exe /wmi:Win32_UserAccount /out:”c:\Win32_UserAccount.cs”          
 
The first switch here tells the tool to create a prototype class for the Win32_UserAccount object. The out switch tells the tool where to save the result. There are a couple more switches
which you can easily query by passing no parameters at all.
 
And yes,  I made the same mistake as Dlinq I used attributes for mapping :) If I can find the time the next version of WMILinq it will have support for namespaces, WMI events, method invocations, associations and probably XML based mapping.
 

Remember you'll need VS2005 RTM and Linq RTM.
 
Some related reading material on this subject
 
 
The folder documentation contains a (dutch) walktrough. I will write an english one tonight.
 
Filed under:

# re: Query your WMI with ease using WMILinq!

Tuesday, December 13, 2005 5:44 AM by Scott Bellware

Very cool! Looking forward to some free time to dig into the query parser. I want to create one of these for a project I'm working on, but so far it's as clear as mud. Your example is much appreciated!

# re: Query your WMI with ease using WMILinq!

Wednesday, December 14, 2005 7:44 AM by Emile Bosch

Thanks for your reply Scott! Make sure to mail me if you have any questions.

# WMILINQ

Thursday, January 26, 2006 2:04 PM by Sahil Malik [MVP C#]

# Links to LINQ

Wednesday, October 04, 2006 6:38 PM by Charlie Calvert's Community Blog

Here are some useful links to LINQ information. Core Microsoft LINQ Sites · Try it yourself: download

# Community Convergence XXVII

Sunday, May 13, 2007 11:13 PM by Charlie Calvert's Community Blog

Welcome to the 27th Community Convergence. I use this column to keep you informed of events in the C#

# An Updated LINQ to WMI Implementation

Wednesday, January 02, 2008 8:56 AM by Eden Ridgway's Blog

An Updated LINQ to WMI Implementation

# Link to Everything: A List of LINQ Providers

Thursday, February 28, 2008 8:49 PM by Charlie Calvert's Community Blog

I've recently updated the list of LINQ Providers found on my Links to LINQ page, accessible from the

# Link to Everything: A List of LINQ Providers

Thursday, February 28, 2008 9:28 PM by Noticias externas

I&#39;ve recently updated the list of LINQ Providers found on my Links to LINQ page, accessible from

# 万般皆LINQ

Saturday, March 01, 2008 1:44 PM by TerryLee

# Hecgo.com &raquo; Linq to ... everything: A List of LINQ Providers

Pingback from  Hecgo.com  &raquo; Linq to ... everything: A List of LINQ Providers

# LINQ To ...

Wednesday, March 19, 2008 8:09 AM by Hilton Giesenow's Jumbled Mind

I mentioned in a post a little while ago about the various LINQ To projects I had seen, but Charlie Calvert

# Providers para Linq

Saturday, March 22, 2008 3:50 PM by Carlos Fernando Paleo da Rocha
SBS MVP in Brazil

LINQ Providers LINQ to Amazon LINQ to Active Directory LINQ over C# project LINQ to CRM LINQ To Geo

# Providers para Linq

Saturday, March 22, 2008 4:19 PM by Tecnologias

LINQ Providers LINQ to Amazon LINQ to Active Directory LINQ over C# project LINQ to CRM LINQ To Geo

# Jacques Snyman &raquo; LINQ To &#8230;

Thursday, March 27, 2008 2:23 PM by Jacques Snyman » LINQ To …

Pingback from  Jacques Snyman &raquo; LINQ To &#8230;

# Linq to everything

Wednesday, April 09, 2008 12:51 AM by Gerry Heidenreich

Linq to everything

# LINQ to [AnyWhere]

Thursday, April 10, 2008 12:38 PM by web-crossing Blog

LINQ to [AnyWhere]

# LINQ to [AnyWhere]

Tuesday, April 22, 2008 11:13 PM by web-crossing Blog

LINQ to [AnyWhere]

# LINQ to [AnyWhere]

Tuesday, April 22, 2008 11:13 PM by LINQ to [AnyWhere]

Pingback from  LINQ to [AnyWhere]

# Links to LINQ

Saturday, September 20, 2008 2:41 AM by Charlie Calvert's Community Blog

Here are some useful links to LINQ information. Use the comments or write me if you want to add to this

# LINQ to ...

Tuesday, November 11, 2008 8:00 PM by Alex Krakovetskiy's blog

Офіційні: LINQ to SQL (DLINQ) LINQ to XML (XLINQ) LINQ to XSD LINQ to Entities BLINQ PLINQ Неофіційні

# LINQ to ...

Офіційні: LINQ to SQL (DLINQ) LINQ to XML (XLINQ) LINQ to XSD LINQ to Entities BLINQ PLINQ Неофіційні

# A List of LINQ Providers &laquo; vincenthome&#8217;s Tech Clips

Saturday, November 29, 2008 6:31 PM by A List of LINQ Providers « vincenthome’s Tech Clips

Pingback from  A List of LINQ Providers « vincenthome’s Tech Clips

# LINQ-To-Everywhere – List of LINQ Providers

Monday, April 27, 2009 11:25 AM by knom's developer corner

This weekend I’ve built a small application, which queries the “Simpsons” seasons guide data and updates

# Tune Up Your PC &raquo; Post Topic &raquo; LINQ-To-Everywhere ??? List of LINQ Providers

Pingback from  Tune Up Your PC  » Post Topic   » LINQ-To-Everywhere ??? List of LINQ Providers

# 万般皆LINQ

Thursday, November 26, 2009 6:03 AM by 周骏

摘要:微软在.NET3.5中推出了LINQ,现在各种LINQProvider满天飞,TerryLee在老外站点上收集了一份LINQProvider列表 微软在.NET3.5中推出了L...

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 5 and 6 and type the answer here: