Author Posts

January 13, 2017 at 10:33 pm

Now that PowerShell is cross-platform is there a way to get the OS and Version? I would like to send that information in the User-Agent parameter of my Invoke-RestMethod.

January 13, 2017 at 10:51 pm

So, it's probably worth knowing that PowerShell CORE is what's cross-platform, not "all" PowerShell. And PowerShell is separate from, say, CIM/WMI/OMI. It's also worth pointing out that PowerShell is intended to act as a kind of a wrapper around whatever the OS does natively – not necessarily replace what the OS does natively.

So let's so you're on Linux. You can't use Get-WmiObject, obviously. However, if your Linux has OMI installed, you could maybe use Get-CimInstance. If your distro implements the CIM_OperatingSystem class, for example, that would tell you.

Also have a look at https://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/. There are several commands, which you can obviously run inside PowerShell, that would get you the OS info. Linux tends to keep everything in text files, so as long as you know which text file to look in, retrieving information isn't usually too hard.

January 14, 2017 at 4:39 pm

Here are a few options I got from twitter.

Add-Type -AssemblyName Systm.Runtime.InteropServices.RuntimeInformation
[Systm.Runtime.InteropServices.RuntimeInformation]::OSDescription
$os = 'unknown'

if ($PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows) {
   $os = 'Windows'
} elseif ($IsOSX) {
   $os = 'OSX'
} elseif ($IsLinux) {
   $os = 'Linux'
}
& bash -c `uname`;