user1106741
user1106741

Reputation: 237

WMI Lookup Values

I"m using WMI to retrieve information for a server's operating system. I have the following the query ->

select *  from Win32_OperatingSystem

The results are fine, however for the OperatingSystemSKU, it returns an integer. On the microsoft website it provides the actual value. I was wondering if this look-up table exists or will I have to create my own local table to do the mapping?

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

Upvotes: 0

Views: 2854

Answers (3)

RRUZ
RRUZ

Reputation: 136431

You can extract the valid values for the OperatingSystemSKU property accessing the Values qualifier.

Check the next image (obtained using WDCC) which shows all the qualifiers and values for the OperatingSystemSKU property.

enter image description here

From .Net you must use the QualifierData Class to extract such info.

Check the next C# sample which build a Lookup list using the Qualifier values.

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                List<string> sLookUp = new List<string>();
                ManagementClass manClass = new ManagementClass("Win32_OperatingSystem");
                manClass.Options.UseAmendedQualifiers = true;
                foreach (PropertyData Property in manClass.Properties)
                    if (Property.Name.Equals("OperatingSystemSKU"))                    
                        foreach (QualifierData Qualifier in Property.Qualifiers)
                            if (Qualifier.Name.Equals("Values")) 
                                foreach (String s in (System.String[])Qualifier.Value)
                            sLookUp.Add(s);                   


                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);
                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT OperatingSystemSKU FROM Win32_OperatingSystem");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0} {1}", "OperatingSystemSKU", sLookUp[Convert.ToInt32((UInt32)WmiObject["OperatingSystemSKU"])]);// Uint32
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

Upvotes: 1

Jay
Jay

Reputation: 10128

Here is a (very trimmed) method I wrote a while ago - gets the name and also gets the windows version as a string using Environment.OSVersion - I'm just assuming you want the OS version name here rather than the sku?

/// <summary>
/// Class for getting environment information
/// </summary>
public static class EnvironmentInfo
{
    /// <summary>
    /// Gets environment information by querying the system
    /// </summary>
    public static IEnumerable<string> GetEnvironmentInfo()
    {
        List<string> results = new List<string>();

        SafeUpdateListOfResultsFromInstrumentation("OS Product: {0}", results, "select * from win32_OperatingSystem", "name");
        SafeUpdateListofResults("OS Version: {0}", results, (() => Environment.OSVersion.ToString()));

        return results;
    }

    private static void SafeUpdateListofResults(string format, List<string> results, Func<string> del)
    {
        try
        {
            string str = del.Invoke();
            results.Add(string.Format(format, str));
        }
        catch (Exception)
        {
            //Swallow exception - can't get diagnostic info!
        }
    }

    private static void SafeUpdateListOfResultsFromInstrumentation(string format, List<string> results, string query, string index)
    {
        try
        {
            WqlObjectQuery objectQuery = new WqlObjectQuery(query);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(objectQuery);

            string name, value;

            foreach (ManagementObject managementObject in searcher.Get())
            {
                name = managementObject[index].ToString();
                string[] split1 = name.Split('|');
                value = split1[0];
                results.Add(string.Format(format, value));
            }
        }
        catch (Exception)
        {
            //Swallow exception - can't get diagnostic info!
        }

    }
}

Upvotes: 0

Random IT Guy
Random IT Guy

Reputation: 623

You can look it up like this

string OSname = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString();

Or just use a messagebox.

MessageBox.Show((from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString());

Upvotes: 1

Related Questions