Solved

WMI Questions

Posted on 2011-09-26
10
321 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now