?
Solved

WMI Questions

Posted on 2011-09-26
10
Medium Priority
?
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

764 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