Determine the OS version, Linux and Windows from Powershell

半世苍凉 提交于 2020-12-01 01:31:29

问题


How can I determine the OS type, (Linux, Windows) using Powershell from within a script?

The ResponseUri isn't recognised when this part of my script is ran on a Linux host.

$UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority

So I want an If statement to determine the OS type that would look similar to this:

If ($OsType -eq "Linux")
{
     $UrlAuthority = ($Request.BaseResponse).RequestMessage | Select-Object -ExpandProperty RequestUri | Select-Object -ExpandProperty host
}
Else
     $UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority

I could use Get-CimInstance Win32_OperatingSystem but it would fail on Linux as it's not recognised.


回答1:


Aren't there environment variables you can view on the other platforms for the OS?

Get-ChildItem -Path Env:

Particularly, on Windows at least, there's an OS environment variable, so you should be able to accomplish this by using $Env:OS.


Since some time has passed and the PowerShell Core (v6) product is GA now (the Core branding has been dropped as of v7), you can more accurately determine your platform based on the following automatic boolean variables:

$IsMacOS
$IsLinux
$IsWindows



回答2:


Since the PowerShell versions 6.1 on Windows/Linux/OSX went to GA you can use the new properties of $PSVersionTable, OS, Platform and GitCommitId

Update In v6.0.0-beta.3 there are some breaking changes:

  • Change positional parameter for powershell.exe from -Command to -File

$PSVersionTable on :

Platform Win32NT OS Microsoft Windows 10.0.15063

PS C:\Users\LotPings> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Platform Unix OS Linux (ubuntu)

PS /home/LotPings> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Linux 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Platform Unix OS Darwin

PS /Users/LotPings> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Darwin 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RE...
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0



回答3:


For PowerShell Core (Powershell Version 6.0+), you can use Automatic Variables: $IsLinux, $IsMacOS and $IsWindows.

For example,

if ($IsLinux) {
    Write-Host "Linux"
}
elseif ($IsMacOS) {
    Write-Host "macOS"
}
elseif ($IsWindows) {
    Write-Host "Windows"
}



回答4:


Actually, there should be global variables added by the PowerShell console itself--they're not considered environment variables though, which is why they wouldn't show up when using dir env: to get a list.The OS-specific ones I see for now are $IsLinux, IsMacOS and $IsWindows. This is of at least PowerShell version 6.0.0-rc and above for Mac/Linux.

You can see a list of what's available by using just Get-Variable (in a fresh session without loading your profile, if you just want what comes build-in by default).




回答5:


When you only have to check if it is windows or linux, maybe you could use this (quick and dirty):

if ([System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue))
{
    #windows
}
else
{
    #Not windows
}



回答6:


Building on the above, if you only want to detect whether or not you're running under Windows, and you want a script that's forwards and backwards compatible in PowerShell and PowerShell Core, there's this:

if ($IsWindows -or $ENV:OS) {
    Write-Host "Windows"
} else {
    Write-Host "Not Windows"
}



回答7:


Some more ways for Osx:

sw_vers -productVersion

10.12.6

Or (there's a "key - os_version" right above it, but I don't see how they relate):

[xml]$xml = system_profiler SPSoftwareDataType -xml   
$xml.plist.array.dict.array.dict.string -match 'macos'  

macOS 10.12.6 (16G1510)



回答8:


This will work in any version of Powershell for the problems described in the comments on other answers.

$iswin = $PSVersionTable.Platform -match '^($|(Microsoft )?Win)'

With $False being 'nix.



来源:https://stackoverflow.com/questions/44703646/determine-the-os-version-linux-and-windows-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!