Solved

WMI Questions

Posted on 2011-09-26
10
318 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
Comment Utility
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
Comment Utility
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
Comment Utility
Also, have you learned how to use WMI for remote machines?
0
 
LVL 4

Author Comment

by:asp_net2
Comment Utility
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
Comment Utility
Thanks
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
Ok, could you possible provide some links for that sort of stuff?
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
Ok, thanks again :)
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

13 Experts available now in Live!

Get 1:1 Help Now