WMI queries comparison: C# vs. PowerShell

Don't get me wrong, I love C#, but PowerShell's built-in support for WMI is so nice.  Even not using [wmi] (or wmiclass, or wmisearcher, etc.) and just get-wmiobject's -query, it's so concise and nice.

Today's example snippets should be relatively obvious in what it does to those familiar with TFS app tiers.  However, since I didn't know how to do WMI queries in C# (I hadn't had a need to use ManagementObjectSearcher in System.Management as of yet), it's also an example for others that may have the same need.

Admittedly, this isn't a language difference, really - if my C# app was already hosting the PowerShell engine, I could call that cmdlet almost as easily as PowerShell can.  Much like other cases, it isn't really the language, but the framework, that gives you the power (which is why people claiming C# as "more powerful" than VB.NET are so funny to listen to), and PowerShell just happens to take the .NET framework that those languages have and add on to it lots of very useful things, especially for verticals like administration :)

PowerShell version

 get-wmiobject -namespace "root/microsoftiisv2" -query 'select * from IIsApplicationPoolSetting where name="W3SVC/AppPools/TeamFoundationPool"' | select wamusername
get-wmiobject -query 'select * from win32_service where Name="CoverAn"' | select StartName
get-wmiobject -query 'select * from win32_service where Name="TFSServerScheduler"' | select StartName

C# version

using System;
using System.Management;

namespace QueryWMI
{
    class Program
    {
        static void Main(string[] args)
        {
            QueryAppPool("W3SVC/AppPools/TeamFoundationPool");
            QueryService("CoverAn");
            QueryService("TFSServerScheduler");
        }

        private static void QueryAppPool(string appPoolName)
        {
            string query = String.Format("select * from IIsApplicationPoolSetting where Name='{0}'", appPoolName);
            string queryScope = "root/MicrosoftIISv2";
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryScope, query))
            using (ManagementObjectCollection results = searcher.Get())
            {
                foreach (ManagementObject result in results)
                {
                    using (result)
                    {
                        Console.WriteLine("App Pool {0} is running as: {1}", appPoolName, result.GetPropertyValue("WAMUsername"));
                    }
                }
            }
        }

        private static void QueryService(string serviceName)
        {
            string query = String.Format("select * from win32_service where Name='{0}'", serviceName);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            using (ManagementObjectCollection results = searcher.Get())
            {
                foreach (ManagementObject result in results)
                {
                    using (result)
                    {
                        Console.WriteLine("Service {0} is running as: {1}", serviceName, result.GetPropertyValue("StartName"));
                    }
                }
            }
        }
    }
}