32

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

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

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

因此,我想要一个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

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

4

10 回答 10

61

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

Get-ChildItem -Path Env:

特别是,至少在 Windows 上,有一个操作系统环境变量,所以你应该能够通过使用$Env:OS.


由于一段时间过去了,PowerShell Core (v6) 产品现在是 GA(从v7 开始, Core品牌已被删除),您可以根据以下自动布尔变量更准确地确定您的平台:

$IsMacOS
$IsLinux
$IsWindows
于 2017-06-22T15:35:53.813 回答
11

由于Windows/Linux/OSX上的 PowerShell 版本 6.1进入 GA,您可以使用$PSVersionTableOSPlatform的新属性GitCommitId

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

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

$PSVersionTable在 :

平台Win32NT操作系统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

平台Unix操作系统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

平台Unix操作系统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
于 2017-06-22T16:20:35.973 回答
6

对于PowerShell Core(Powershell 版本 6.0+),您可以使用自动变量:$IsLinux和.$IsMacOS$IsWindows

例如,

if ($IsLinux) {
    Write-Host "Linux"
}
elseif ($IsMacOS) {
    Write-Host "macOS"
}
elseif ($IsWindows) {
    Write-Host "Windows"
}
于 2019-02-12T08:23:10.237 回答
5

实际上,应该有 PowerShell 控制台本身添加的全局变量——尽管它们不被视为环境变量,这就是为什么它们在dir env:用于获取列表时不会出现的原因。我现在看到的特定于操作系统的变量是$IsLinux,IsMacOS$IsWindows. 对于 Mac/Linux,这至少是 PowerShell 版本 6.0.0-rc 及更高版本。

您可以通过使用来查看可用内容的列表Get-Variable(如果您只想要默认内置的内容,则在不加载个人资料的新会话中)。

于 2018-03-26T14:31:30.150 回答
5

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

if ($IsWindows -or $ENV:OS) {
    Write-Host "Windows"
} else {
    Write-Host "Not Windows"
}
于 2019-09-23T21:17:32.267 回答
3

当您只需要检查它是 windows 还是 linux 时,也许您可​​以使用这个(快速而肮脏):

if ([System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue))
{
    #windows
}
else
{
    #Not windows
}
于 2017-06-22T15:39:58.640 回答
1

Osx 的更多方法:

sw_vers -productVersion

10.12.6

或者(在它上面有一个“key - os_version”,但我看不出它们是如何关联的):

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

macOS 10.12.6 (16G1510)
于 2019-12-30T04:37:28.763 回答
1

对于其他答案的评论中描述的问题,这将适用于任何版本的 Powershell。

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

随着$False'nix。

于 2020-09-05T13:33:38.410 回答
1

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

[System.Environment]::OSVersion.Platform

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

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

function Get-PSPlatform
{
    return [System.Environment]::OSVersion.Platform
}
switch (Get-PSPlatform)
{
    'Win32NT' { 
        New-Variable -Option Constant -Name IsWindows -Value $True -ErrorAction SilentlyContinue
        New-Variable -Option Constant -Name IsLinux  -Value $false -ErrorAction SilentlyContinue
        New-Variable -Option Constant -Name IsMacOs  -Value $false -ErrorAction SilentlyContinue
     }
}

这适用于所有版本的 PowerShell,因为从版本 1.x 起,.NET 就可以使用它。有关详细信息,请参阅PlatformID 文档

— 请参阅Dave F 的评论;我写了这个答案,因为这似乎是如何从评论中获得答案的。

于 2021-06-23T19:18:55.453 回答
0

如果您没有安装最新的 PowerShell 核心,您可以使用一个小脚本块,例如:

if ($PSVersionTable.PSVersion.Major -lt 6.0) {
    switch ($([System.Environment]::OSVersion.Platform)) {
        'Win32NT' { 
            New-Variable -Option Constant -Name IsWindows -Value $True -ErrorAction SilentlyContinue
            New-Variable -Option Constant -Name IsLinux -Value $false -ErrorAction SilentlyContinue
            New-Variable -Option Constant -Name IsMacOs -Value $false -ErrorAction SilentlyContinue
        }
    }
}
$script:IsLinuxEnv = (Get-Variable -Name "IsLinux" -ErrorAction Ignore) -and $IsLinux
$script:IsMacOSEnv = (Get-Variable -Name "IsMacOS" -ErrorAction Ignore) -and $IsMacOS
$script:IsWinEnv = !$IsLinuxEnv -and !$IsMacOSEnv
于 2022-01-24T15:01:23.187 回答