Solved

WMI Questions

Posted on 2011-09-26
10
322 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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