Translate

Wednesday, April 2, 2014

C# Introduction to WMI (Windows Management Instrumentation)

All the information about your system (such as how much free space is left on the disk, what is the current CPU utilization, what type of network adapter is installed, what applications are installed on your computer and so on), is stored in a repository called CIM repository. WMI is an API provided by windows, using which you can query this database (CIM repository) that holds your system information. You can use the WMI interface to query both local and remote computers. The classes in C#, to access this API, is under the System.Management namespace.


Create a new console application and copy paste this code. In the example below, I am querying the local machine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;

namespace WMIIntroduction
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var managementBaseObjects = new ManagementObjectSearcher("Select * from win32_diskdrive");//Add reference to System.Management

            foreach (var managementBaseObject in managementBaseObjects.Get())
            {
                foreach (var property in managementBaseObject.Properties)
                {
                    Console.WriteLine("***********************************");
                    Console.WriteLine(property.Name);
                    Console.WriteLine(property.Value);
                }
            }
        }
        
    }
}

In case you are interested to know, you can run commands directly against the WMI API (bypassing the .net framework) using Windows Powershell.

Example:

get-wmiobject -query  "Select * from win32_diskdrive"           


Code to query a remote machine using WMI

In the code below, replace the red text with appropriate values


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management;
using System.Security.AccessControl;

public class RemoteConnect
{


    public static void Main()
    {
        var connectionOptions = new ConnectionOptions
        {
            Username = @"domain\username",
            Password = "password",
            Impersonation = ImpersonationLevel.Impersonate,
            Authentication = AuthenticationLevel.Packet
        };

        var path = new ManagementPath
        {
            ClassName = "",
            NamespacePath = "root\\cimv2",
            Path = @"\\serverdnsname\root\cimv2",
            RelativePath = "",
            Server = "serverdnsname"
        };


        var mgmtScope =
            new ManagementScope(
                path, connectionOptions);

        mgmtScope.Connect();
        Console.WriteLine(mgmtScope.IsConnected);
        var query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        var searcher = new ManagementObjectSearcher(mgmtScope, query);
 var managementBaseObjects = searcher.Get();

      foreach (var managementBaseObject in managementBaseObjects)
        {
            foreach (var property in managementBaseObject.Properties)
            {
                               Console.WriteLine("***********************************");
                Console.WriteLine(property.Name);
                Console.WriteLine(property.Value);
            }
        }


    }


}