tags:

views:

323

answers:

2

I am trying to get information on the version of Windows installed from WMI. Most fields work. I can get the operating system "Name" as well as the "Version", both are fields of the Win32_OperatingSystem object I have.

But another field "OSArchitecture" generates an exception ("Not found").

    strScope = "\\" + strServer + "\root\CIMV2"
    searcher = New ManagementObjectSearcher(strScope, "SELECT * FROM Win32_OperatingSystem")

    For Each mo In searcher.Get

        strOSName = mo("Name")
        strOSVersion = mo("Version")
        strOSArchitecture = mo("OSArchitecture")
        strStatus = mo("Status")
        strLastBoot = mo("LastBootUpTime")

    Next

The documentation says that the field ought to exist and is a String:

http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx

Any ideas?

+1  A: 

Your original question had the line:

strOSArchitecture = mo("Architecture")

which should have been:

strOSArchitecture = mo("OSArchitecture")

Now that you've confirmed that was a simple typo in the question (not your actual code), the other likelihood is that you are running on either Server 2003, 2000, NT4, XP or Me/98/95, where the documentation lists the OSArchitecture key as unavailable?

paxdiablo
Sorry, it actually did. I pasted the wrong version of the code after I was trying to fix this by trying out different names. The exception happens with "OSArchitecture". I'll edit my question.
Andrew J. Brehm
I am realising now that it depends on the target machine. I wrote a small program moTest using Gimel's idea and ran it against a 2003 and a 2008 machine. The 2008 machine returned a mo that had an OSArchitecture property and the 2003 machine returned a mo that had no OSArchitecture property.
Andrew J. Brehm
Can you edit your answer to remove the correction of the property name and contain only the actual answer?
Andrew J. Brehm
+1  A: 

To view a current (runtime) list of available properties, walk the Properties attribute. In a console application, it looks like:

For Each mo In searcher.Get
    Console.WriteLine("..." + mo.Properties.Count.ToString() + " properties")
    For Each prop In mo.Properties
        Console.WriteLine(prop.Name)
    Next
    '...

On my XP installation, no OSArchitecture appears in the 61 property names listed.

gimel
That would explain the exception. But since the actual native WMI class defines such a field (OSArchitecture) why wouldn't it be there as a property? Bug in the bridge?
Andrew J. Brehm