0

I'm making a little program to check some specifications of the CPU, like RAM, CPU name, Speed, etc., but my knowledge is basic, and im using powershell in Visual Studio with C#, so im using this method to bring the OS name, at the moment, it works, but specifically in a pc, it doesn't work, it brings a null value. I have tested it on so many other pc's, and it's working, but i have no idea why on that pc doesn't. This is my method:

public String RtrOS()
    {
        String OS = "";
        PowerShell powershellCommand = PowerShell.Create();
        powershellCommand.AddScript("(Get-WmiObject Win32_OperatingSystem).Version");
        Collection<PSObject> results = powershellCommand.Invoke();
        foreach (var result in results)
        {
            OS = result.ToString();
        }
    }

So, what i need it's to know if my method it's fine, and if it possible to do it in another way more efficient. This isn't the full method, i use another to return the name with a validation checking the number of the version that this method returns. The problem is, that for some reason, in only a pc it isn't working.

Kevin M.
  • 33
  • 6
  • Try `[System.Environment]::OSVersion.Version.ToString()` or without using PowerShell at all you can use `System.Environment.OSVersion.Version.ToString()` in C#. There is also `System.Environment.OSVersion.VersionString` which will give you back something like `"Microsoft Windows NT 10.0.19043.0"` – Daniel Sep 13 '21 at 15:41
  • 4
    why call powershell from c# for such a trivial task, query wmi directly from c# instead, see example: https://learn.microsoft.com/en-us/windows/win32/wmisdk/retrieving-a-class – Avshalom Sep 13 '21 at 16:05
  • [Windows - GET OS Version / friendly name in c#](https://stackoverflow.com/a/6331863/2791540) – John Wu Sep 13 '21 at 17:17

0 Answers0