从Powershell确定操作系统版本,Linux和Windows

boo*_*ist 15 linux windows powershell operating-system

如何在脚本中使用Powershell确定操作系统类型(Linux,Windows)?

当我的脚本的这部分在Linux主机上运行时,无法识别ResponseUri.

$UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority
Run Code Online (Sandbox Code Playgroud)

所以我想要一个If声明来确定与此类似的操作系统类型:

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
Run Code Online (Sandbox Code Playgroud)

我可以使用,Get-CimInstance Win32_OperatingSystem但它会在Linux上失败,因为它无法识别.

The*_*le1 24

您是否可以在操作系统的其他平台上查看环境变量?

Get-ChildItem -Path Env:
Run Code Online (Sandbox Code Playgroud)

特别是,至少在Windows上,有一个OS环境变量,所以你应该能够通过使用来实现这一点$Env:OS


由于已经过了一段时间并且PowerShell Core产品是GA now(6.0.*),您可以根据以下自动布尔变量更准确地确定您的平台:

$IsMacOS
$IsLinux
$IsWindows
Run Code Online (Sandbox Code Playgroud)

  • @DaveF由于PSv6已经过GA,现在有`$ IsMacOS`和`$ IsLinux`自动变量可以确定你的操作系统. (4认同)
  • 使用`[System.Environment] :: OSVersion.Platform`。它记录在[MSDN for并且在所有.NET版本(1.1、2.0、3.0、3.5和当前版本)中都存在]](https://msdn.microsoft.com/zh-cn/library/system .environment.osversion%28v = vs.110%29.aspx)。$ PSVersionTable.Platform` [在PowerShell 6 Core中返回该值](https://docs.microsoft.com/zh-cn/powershell/scripting/whats-new/what-s-new-in-powershell-core- 60?view = powershell-6#engine-updates)。在Windows上将是Win32NT,在Linux和macOS上将是Unix。 (3认同)
  • @boomcubist我想到了一个更简单的检查(并且无法编辑我的其他评论):`if($ env:OS)`因为它只有两个状态 (2认同)

gae*_*ard 10

在 PowerShell [Core] 版本 6 之前,这只能通过直接询问 .NET 来实现。这可以用一行完成:

\n
[System.Environment]::OSVersion.Platform\n
Run Code Online (Sandbox Code Playgroud)\n

Win32NT对于从 Windows NT(Windows 的所有当前版本)派生的任何内容或Unix任何 *nix(包括 Mac、Linux 等)都将返回此值。如果它返回,Unix那么您显然正在运行 v6+,因此可以从$PSVersionTable.PSEdition$PSVersionTable.Platform和获得更多信息$PSVersionTable.OS,并且自动变量也将可用:$IsLinux$IsMacOs$IsWindows

\n

这是我profile.ps1通过设置使这变得更容易的内容$IsWindows

\n
function Get-PSPlatform\n{\n    return [System.Environment]::OSVersion.Platform\n}\nswitch (Get-PSPlatform)\n{\n    \'Win32NT\' { \n        New-Variable -Option Constant -Name IsWindows -Value $True -ErrorAction SilentlyContinue\n        New-Variable -Option Constant -Name IsLinux  -Value $false -ErrorAction SilentlyContinue\n        New-Variable -Option Constant -Name IsMacOs  -Value $false -ErrorAction SilentlyContinue\n     }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这适用于所有版本的 PowerShell,因为自 1.x 版本以来,.NET 就提供了此功能。有关详细信息,请参阅PlatformID 文档。

\n

\xe2\x80\x94\n请参阅Dave F\ 的评论;我写这个答案是因为这似乎是如何从评论中获得答案的。

\n


小智 8

由于PowerShell的版本6.1上的Windows/Linux的/ OSX去GA可以使用的新特性$PSVersionTable,OS,PlatformGitCommitId

更新在v6.0.0-beta.3中有一些breaking changes:

  • 将powershell.exe的位置参数从-Command更改为-File

$PSVersionTable 上 :

平台Win32NTOSMicrosoft 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
Run Code Online (Sandbox Code Playgroud)

平台UnixOSLinux (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
Run Code Online (Sandbox Code Playgroud)

平台UnixOSDarwin

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
Run Code Online (Sandbox Code Playgroud)

  • 在版本6之前的PowerShell上运行时,这将无济于事. (2认同)
  • @lit 我相信`[System.Environment]::OSVersion.Platform` 适用于所有版本。有关已接受的答案,请参阅我的评论。 (2认同)

zwc*_*oud 6

对于PowerShell核心(Powershell的版本6.0+),你可以使用自动变量$IsLinux$IsMacOS$IsWindows

例如,

if ($IsLinux) {
    Write-Host "Linux"
}
elseif ($IsMacOS) {
    Write-Host "macOS"
}
elseif ($IsWindows) {
    Write-Host "Windows"
}
Run Code Online (Sandbox Code Playgroud)


MCa*_*tle 6

基于上述内容,如果您只想检测是否在 Windows 下运行,并且想要一个在 PowerShell 和 PowerShell Core 中向前和向后兼容的脚本,则可以使用以下代码:

if ($IsWindows -or $ENV:OS) {
    Write-Host "Windows"
} else {
    Write-Host "Not Windows"
}
Run Code Online (Sandbox Code Playgroud)


are*_*ing 5

实际上,PowerShell控制台本身应该添加了全局变量-但是它们不被视为环境变量,这就是为什么它们在dir env:用于获取列表时不会显示的原因。我现在看到的特定于OS的变量是$IsLinuxIsMacOS$IsWindows。对于Mac / Linux,这至少是PowerShell版本6.0.0-rc及更高版本。

您可以通过仅使用来查看可用列表Get-Variable(如果您只想要默认内置的功能,则在不加载配置文件的全新会话中)。