.NETでWMIを用いてOSのバージョン情報、Hotfix、ドライブ情報を取得する

まず、System.Managementの参照を追加してから。

using System;
using System.IO;
using System.Linq;
using System.Management;

class Program
{
    private static string remoteUserName;
    private static string remotePassword;
    private static string remoteServerName;

    static void Main(string[] args)
    {
        try
        {
            remoteUserName = "XXXXX";
            remotePassword = "YYYYY";
            remoteServerName = "ZZZZZ";

            ShowLocalOSVersion();
            ShowRemoteOSVersion();
            ShowWindowsHotFix();
            ShowLocalDiskInfo();
            ShowRemoteDiskInfo();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            Console.ReadLine();
        }
    }

    private static void ShowLocalOSVersion()
    {
        var query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        var searcher = new ManagementObjectSearcher(query);
        ShowOSVersion(searcher);
    }

    private static ManagementScope GetRemoteScope()
    {
        var options = new ConnectionOptions();
        options.Username = string.Join(@"\", remoteServerName, remoteUserName);
        options.Password = remotePassword;

        var scope = new ManagementScope();
        scope.Path.Server = remoteServerName;
        scope.Options = options;
        return scope;
    }

    private static void ShowRemoteOSVersion()
    {
        var query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        var searcher = new ManagementObjectSearcher(GetRemoteScope(), query);
        ShowOSVersion(searcher);
    }

    private static void ShowOSVersion(ManagementObjectSearcher searcher)
    {
        var moc = searcher.Get();

        var spName = "";
        foreach (var mo in moc)
        {
            var osCap = mo["Caption"] == null ? "" : mo["Caption"].ToString();
            var spCap = mo["CSDVersion"] == null ? "" : mo["CSDVersion"].ToString();
            Console.WriteLine("{0} {1}[{2}]", osCap, spName, Environment.OSVersion.Version);
            // => e.g. Microsoft Windows 8 Pro[6.2.9200.0]
        }
    }

    private static void ShowWindowsHotFix()
    {
        var query = new ObjectQuery("SELECT * FROM Win32_QuickFixEngineering");
        var searcher = new ManagementObjectSearcher(query);
        var moc = searcher.Get();

        foreach (var mo in moc)
        {
            var description = mo["Description"] == null ? "" : mo["Description"].ToString();
            var hotFixID = mo["HotFixID"] == null ? "" : mo["HotFixID"].ToString();
            Console.WriteLine("{0}:{1}", description, hotFixID);
            // => e.g. Security Update:KB9999999
        }
    }

    private static void ShowLocalDiskInfo()
    {
        //ローカルは WMI 使わなくても .NET で取れる
        var drives = from drive in DriveInfo.GetDrives()
                     where drive.IsReady && drive.DriveType == DriveType.Fixed
                     select drive;

        foreach (var drive in drives)
        {
            var di = new DriveInfo(drive.Name);
            //サイズは丸めてみる
            var totalSize = DiskSizeRound(di.TotalSize);
            var totalFreeSpace = DiskSizeRound(di.TotalFreeSpace);
            Console.WriteLine("{0} TotalSize:{1} TotalFreeSpace:{2}", drive.Name, totalSize, totalFreeSpace);
        }
    }

    private static void ShowRemoteDiskInfo()
    {
        var query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk Where DriveType=3");
        var searcher = new ManagementObjectSearcher(GetRemoteScope(), query);
        var moc = searcher.Get();

        foreach (var mo in moc)
        {
            var driveName = mo["DeviceID"] == null ? "" : mo["DeviceID"].ToString();
            var totalSize = mo["Size"] == null ? "" : mo["Size"].ToString();
            var totalFreeSpace = mo["FreeSpace"] == null ? "" : mo["FreeSpace"].ToString();
            Console.WriteLine("{0} TotalSize:{1} TotalFreeSpace:{2}", driveName, totalSize, totalFreeSpace);
        }
    }

    private static string DiskSizeRound(long target)
    {
        if (target / 1024 / 1024 > 1000)
            return Math.Round((decimal)(target / 1024 / 1024 / 1024), 1).ToString("#,0.#") + "GB";
        else
            return Math.Round((decimal)(target / 1024 / 1024), 1).ToString("#,0.#") + "MB";
    }
}


WMIって知らなかった。色々取れて面白い。