Laden...

WMI-Reader

Erstellt von Neals vor 13 Jahren Letzter Beitrag vor 13 Jahren 5.061 Views
N
Neals Themenstarter:in
203 Beiträge seit 2008
vor 13 Jahren
WMI-Reader

Beschreibung:

Dieses kleine Tool bietet die Möglichkeit Informationen über das System mit Hilfe der WMI (Windows Management Instrumentation) auszulesen. Es wird eine Auflistung der Komponenten angezeigt, von der dann eine ausgewählt und angezeigt werden kann. Die WMI ist eine Erweiterung des CIM (Common Information Models) und wird zur Administration und Fernwartung von Windows-Systemen verwendet.

Weitere Informationen:
Wikipedia: WMI
MSDN: WMI


namespace WmiReader
{
    public class Wmi
    {
        public string Read(string item)
        {
            var builder = new StringBuilder();

            try
            {
                using (var management = new ManagementClass(item))
                {
                    using (var collection = management.GetInstances())
                    {
                        if (collection.Count > 0)
                        {
                            builder.AppendLine(string.Format("{0}", item));
                            WriteManagementObjects(builder, collection);
                        }
                        else
                        {
                            builder.AppendLine("Not Items found!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                builder.AppendLine("Exception occured!");
                builder.AppendLine(string.Format("{0}: {1}", ex, ex.Message));
            }

            return builder.ToString();
        }

        private static void WriteManagementObjects(StringBuilder builder, ManagementObjectCollection collection)
        {
            if (builder == null)
            {
                return;
            }

            if (collection == null)
            {
                return;
            }

            var counter = 1;

            foreach (ManagementObject obj in collection)
            {
                builder.AppendLine(string.Format("\t{0}", counter++));

                builder.AppendLine(string.Format("\t\tProperties:"));

                foreach (var data in obj.Properties)
                {
                    WritePropertyData(builder, data);
                }

                builder.AppendLine(string.Format("\t\tSystemProperties:"));

                foreach (var data in obj.SystemProperties)
                {
                    WritePropertyData(builder, data);
                }
            }
        }

        private static void WritePropertyData(StringBuilder builder, PropertyData data)
        {
            if (builder == null)
            {
                return;
            }

            if (data == null)
            {
                return;
            }

            if (data.Value != null)
            {
                builder.AppendLine(string.Format("\t\t\t{0}: {1}", data.Name, data.Value));
            }
        }

        public static List<string> Win32Classes = new List<string>
        {
            "Win32_Account",
            "Win32_BIOS",
            "Win32_BootConfiguration",
            "Win32_Bus",
            "Win32_CacheMemory",
            "Win32_CDROMDrive",
            "Win32_ComputerShutdownEvent",
            "Win32_ComputerSystem",
            "Win32_ComputerSystemProcessor",
            "Win32_ComputerSystemProduct",
            "Win32_ComputerSystemWindowsProductActivationSetting",
            // ... Komplette Liste im Anhang zu finden.
        };
    }
}

Schlagwörter: WMI, CIM, Windows, System, Informationen

Signatur.Text = "Greetz, Neals";

N
Neals Themenstarter:in
203 Beiträge seit 2008
vor 13 Jahren

Hier die Sourcen...

Signatur.Text = "Greetz, Neals";

795 Beiträge seit 2006
vor 13 Jahren
`There are 10 types of people in the world: Those, who think they understand the binary system Those who don't even have heard about it And those who understand "Every base is base 10"`
1.820 Beiträge seit 2005
vor 13 Jahren

Hallo!

@Neals:
Nettes Tool, wenn's auch - wie bereits bemerkt - ähnliche bereits gibt.
Eine interessante Erweiterung wäre, dass Ganze also Komponente zu erstellen, welche auch Zugriff auf Remote-WMI's ermöglicht, und man darauf aufbauend einfache Wrapper schreiben könnte, um z.B. die CPU-Zahl eines entfernten Rechners einfach ermitteln zu können:


var wmi = new Wmi("host", "user", "password");
var cpu = new WmiCpu(wmi);
Console.WriteLine(cpu.Count);

Nobody is perfect. I'm sad, i'm not nobody 🙁

888 Beiträge seit 2007
vor 13 Jahren

Hier noch was ähnliches:
CodeProject - WMIQuery

Und nochwas richtiges tolles, leider in VB:
CodeProject - WMISysInfo