Solved

WMI Questions

Posted on 2011-09-26
10
326 Views
Last Modified: 2012-06-21
Hello Experts,

I would like to start learning how to use WMI and not sure where to start so I found this article online but not sure how up to date it is. I would like to know the following below.

I also downloaded the WMI Administrative Tools but not sure how to use them.

- Types of Namespaces I need to include in my pages
- How to retrieve information about local and remote systems (Windows Machines).

I would appreciate any GOOD WORKING tutorials or even articles to read to learn from (preferable easy to follow as well).
0
Comment
Question by:asp_net2
  • 6
  • 4
10 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36676619
I have been working with WMI for many, many years, and I am still learning new things about it all the time, so good luck with your journey.  There is so much reference material, that it is difficult to find the perfect ones.

I found Emmet Gray to have a pretty good understanding of WMI.

Introduction to WMI
http://www.emmet-gray.com/Articles/WMI_Intro.htm
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36681709
Hello TheLearnedOne,

Thank you and allow me to share a site that I just found now that has kind of got me going.

http://aspalliance.com/629_Introducing_Windows_Management_Instrumentation_WMI.1

Does Experts Exchange have a WMI Zone?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36681886
Also, have you learned how to use WMI for remote machines?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:asp_net2
ID: 36694936
Hello TheLearnedOne,

I just wanted to share what I learned from the site that I posted above in the event it can help you. Also, you have helped me in the past and was wondering if you could help me with another post in another topic. Please let me know once you recieve this post and I will close it and award points and also supply other post I need help with.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Management;

public partial class index : System.Web.UI.Page
{

    string hardDriveInfo = null;
    string macAddressInfo = null;
    string cpuInfo = null;
    string operatingSystemInfo = null;
    protected System.Web.UI.WebControls.Label Label1;
    string report = null;
    string domain = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Put user code to initialize the page here
        GetHardDriveInfo();
        GetMACAddress();
        GetCPUId();
        GetOperatingSystemInfo();
        GetOperatingSystemDetails();
        GetDomain();
    }

    /// <summary>
    /// Returns Hard Drive Information
    /// </summary>
    /// <returns>[string] Hard Drive Information</returns>
    public string GetHardDriveInfo()
    {
        ManagementClass managementClass = new ManagementClass("Win32_DiskDrive");
        ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
        StringBuilder sbHardDrive = new StringBuilder();

        foreach (ManagementObject managementObject in managementObjectCollection)
        {
            if (hardDriveInfo == null)
            {
                sbHardDrive.Append("Model: " + managementObject["Model"].ToString() +
                    "<br>Type: " + managementObject["InterfaceType"].ToString() +
                    "<br>Device ID: " + managementObject["DeviceID"].ToString() +
                    "<br>Media Type: " + managementObject["MediaType"].ToString() +
                    "<br>Description: " + managementObject["Description"].ToString() +
                    "<br>Manufacturer: " + managementObject["Manufacturer"].ToString());
            }
            managementObject.Dispose();
        }

        HardDriveLabel.Text = sbHardDrive.ToString();
        return hardDriveInfo = sbHardDrive.ToString();
    }

    /// <summary>
    /// Returns MAC Address
    /// </summary>
    /// <returns>[string] MAC Address</returns>
    public string GetMACAddress()
    {
        ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();

        foreach (ManagementObject managementObject in managementObjectCollection)
        {
            if (macAddressInfo == null)  // only return MAC Address from first card
            {
                if ((bool)managementObject["IPEnabled"] == true)
                {
                    macAddressInfo = managementObject["MacAddress"].ToString();
                }
            }
            managementObject.Dispose();
        }
        MacAddressLabel.Text = macAddressInfo;
        return macAddressInfo;
    }

    /// <summary>
    /// Returns Processor Information
    /// </summary>
    /// <returns>[string] Processor Information</returns>
    public string GetCPUId()
    {
        ManagementClass managementClass = new ManagementClass("Win32_Processor");
        ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
        StringBuilder sbCPU = new StringBuilder();

        foreach (ManagementObject managementObject in managementObjectCollection)
        {
            if (cpuInfo == null)
            {
                sbCPU.Append("Clock Speed: " + managementObject["CurrentClockSpeed"].ToString() +
                    "<br>Description: " + managementObject["Description"].ToString() +
                    "<br>Manufacturer: " + managementObject["Manufacturer"].ToString() +
                    "<br>Name: " + managementObject["Name"].ToString() +
                    "<br>Processor ID: " + managementObject["ProcessorId"].ToString());
            }
            managementObject.Dispose();
        }
        CPULabel.Text = sbCPU.ToString();
        return cpuInfo = sbCPU.ToString();
    }

    /// <summary>
    /// Returns Operating System Information
    /// </summary>
    /// <returns>[string] Operating System Information</returns>
    public string GetOperatingSystemInfo()
    {
        ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem");
        ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
        StringBuilder sbOperatingSystem = new StringBuilder();

        foreach (ManagementObject managementObject in managementObjectCollection)
        {
            if (operatingSystemInfo == null)
            {
                sbOperatingSystem.Append("Version: " + managementObject["Version"].ToString() +
                    "<br>Build Number: " + managementObject["BuildNumber"].ToString() +
                    "<br>Build Type: " + managementObject["BuildType"].ToString() +
                    "<br>Manufacturer: " + managementObject["Manufacturer"].ToString() +
                    "<br>Serial Number: " + managementObject["SerialNumber"].ToString() +
                    "<br>Name: " + managementObject["Name"].ToString() +
                    "<br>Service Pack: " + managementObject["CSDVersion"].ToString() +
                    "<br>Workstation Name: " + managementObject["CSName"].ToString() +
                    "<br>Registered User: " + managementObject["RegisteredUser"].ToString() +
                    "<br>System Directory: " + managementObject["SystemDirectory"].ToString() +
                    "<br>System Drive: " + managementObject["SystemDrive"].ToString());
            }
            managementObject.Dispose();
        }

        OperatingSystemLabel.Text = sbOperatingSystem.ToString();
        return operatingSystemInfo = sbOperatingSystem.ToString();
    }

    /// <summary>
    /// Returns A Complete System Information Report
    /// </summary>
    /// <returns>[string] Complete System Information Report</returns>
    public string BuildReport()
    {
        string newHardDriveInfo = hardDriveInfo.Replace("<br>", "\n");
        string newOperatingSystemInfo = operatingSystemInfo.Replace("<br>", "\n");
        string newCpuInfo = cpuInfo.Replace("br>", "\n");
        DateTime dtCurrTime = DateTime.Now;
        StringBuilder sbReport = new StringBuilder();

        sbReport.Append("System Report Generated on " + dtCurrTime +
            "\n\nHARD DRIVE INFORMATION: \n" + newHardDriveInfo +
            "\n\nMAC ADDRESS INFORMATION: \n" + macAddressInfo +
            "\n\nCPU INFORMATION: \n" + newCpuInfo +
            "\n\nOPERATING SYSTEM INFORMATION: \n" + newOperatingSystemInfo);

        return report = sbReport.ToString();
    }

    public ArrayList GetOperatingSystemDetails()
    {
        ArrayList osCollection = new ArrayList();
        ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");

        foreach (ManagementObject osObject in mos.Get())
        {
            IOperatingSystem os = new IOperatingSystem();
            os.BuildNumber = osObject["BuildNumber"].ToString();
            os.BuildType = osObject["BuildType"].ToString();
            os.Version = osObject["Version"].ToString();
            osCollection.Add(os);
        }

        return osCollection;
    }

    public string GetDomain()
    {
        ArrayList csCollection = new ArrayList();
        ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
        StringBuilder sb = new StringBuilder();

        foreach (ManagementObject osObject in mos.Get())
        {
            sb.Append("Domain: " + osObject["Domain"].ToString() +
             "\n\n Computer Name: " + osObject["Name"].ToString());
        }

        Domain.Text = sb.ToString();
        return sb.ToString();
    }
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36709887
Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36711590
Remote execution requires a ManagementScope with ConnectionOptions, so that you can specify a remote machine, and a user with access rights to that machine.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36711862
Ok, could you possible provide some links for that sort of stuff?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36712420
MSDN can actually be of some use:

ManagementScope Class
http://msdn.microsoft.com/en-us/library/21xxae28.aspx
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36712567
Ok, thanks again :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36712659
You will need the handy-dandy reference to the Win32 classes in WMI (it is a very very long list).

Win32 Classes
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394084(v=vs.85).aspx
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 30
How to extract certain lines from textfile and save them to another file? 6 38
VS 2017 18 101
C# XML Get Values 4 33
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question