Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell WMI output doesnt match c# WMI output

Tags:

c#

powershell

wmi

First I would like to say thank you for helping me with this issue. I really appreciate your time and efforts.

The title sums it up pretty well however I will provide a few specifics. Basically if I pull my OS version using C# it returns the result 6.2 which is for Windows 8 even though my system is 8.1 which should return 6.3. After my research I found that this was a documented limitation inside of the System.Enviroment class, ... gotta love those "features".

I have found a way to deal with this by going into the reg and comparing my 6.2 result with the current version key under HKLM\SOFTWARE\Microsoft\WindowsNT\CurrentVersion however, this is a very risky operation as the reg info could change without notice.

It all got screwy on me when I tried to poll WMI though Powershell. I can't remember why I did a WMI search though Powershell, I guess its not just cats that get curious :)


C# Code:


string version = Environment.OSVersion.ToString();
MessageBox.Show(version);
//Output "Microsoft Windows NT 6.2.9200.0"

Powershell Code:


[System.Environment]::OSVersion | Select-Object -Property VersionString
//OUtput "Microsoft Windows NT 6.3.9600.0"

I have tried both x86 and x64 builds of my C# program as well as running the Powershell both x86 and x64. The discrepancies did not change.

This raises several questions for me but the basic one is where does Powershell get the correct info from? Does Powershell use the reg like I had planned to fix its output? Since my build targets .Net 3.5 does Powershell pull .Net 4.5 (changed my build and this changed nothing).

From my understanding [System.Environment]::OSVersion pulls info the same as System.Environment.OSVersion.

How the heck does Powershell work and C# fails??

:) Thanks again!

like image 602
Eddie Avatar asked Nov 20 '14 18:11

Eddie


1 Answers

As far as I can tell only the Environment.Version call in a C# program is incorrect (6.2). In PowerShell it is correct (6.3). In WMI called via PowerShell or C# it is correct. In fact, C# compiled from source in PowerShell using Add-Type returns 6.3.

Looking into the article here (thanks Booga Roo) it indicates that unless your application specifically says it targets Windows 8.1 via a manifest file, you get the old version (6.2).

You can make a C# application return the correct version by adding an application manifest file and declaring 8.1 support by uncommenting the line

<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>

It seems safe to assume that the writers of PowerShell at Microsoft included an application manifest with powershell.exe that declares Windows 8.1 support.

like image 104
Mike Zboray Avatar answered Nov 15 '22 01:11

Mike Zboray