[英]Determine the OS version, Linux and Windows from Powershell
如何在脚本中使用 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 上会失败,因为它无法识别。
没有可以在操作系统的其他平台上查看的环境变量吗?
Get-ChildItem -Path Env:
特别是,至少在 Windows 上,有一个操作系统环境变量,因此您应该能够通过使用$Env:OS
来完成此操作。
由于一段时间过去了, PowerShell Core (v6) 产品现已正式发布(从 v7 开始, Core品牌已被删除),您可以根据以下自动布尔变量更准确地确定您的平台:
$IsMacOS
$IsLinux
$IsWindows
由于Windows/Linux/OSX上的 PowerShell 6.1版已发布,您可以使用$PSVersionTable
、 OS
、 Platform
和GitCommitId
的新属性
更新在 v6.0.0-beta.3 中有一些breaking changes
:
$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
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
平台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
对于PowerShell Core (Powershell 版本 6.0+),您可以使用自动变量: $IsLinux
、 $IsMacOS
和$IsWindows
。
例如,
if ($IsLinux) {
Write-Host "Linux"
}
elseif ($IsMacOS) {
Write-Host "macOS"
}
elseif ($IsWindows) {
Write-Host "Windows"
}
实际上,PowerShell 控制台本身应该添加全局变量——尽管它们不被视为环境变量,这就是为什么在使用dir env:
获取列表时它们不会出现的原因。我看到的特定于操作系统的变量现在是$IsLinux
、 IsMacOS
和$IsWindows
。 这至少是适用于 Mac/Linux 的 PowerShell 版本 6.0.0-rc 及更高版本。
您可以仅使用Get-Variable
来查看可用内容的列表(如果您只想要默认内置的内容,则在不加载配置文件的新会话中)。
当你只需要检查它是 windows 还是 linux 时,也许你可以使用这个(又快又脏):
if ([System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue))
{
#windows
}
else
{
#Not windows
}
在上述基础上,如果您只想检测您是否在 Windows 下运行,并且您想要一个在 PowerShell 和 PowerShell Core 中向前和向后兼容的脚本,有这个:
if ($IsWindows -or $ENV:OS) {
Write-Host "Windows"
} else {
Write-Host "Not Windows"
}
Osx 的更多方法:
sw_vers -productVersion
10.12.6
或者(在它的正上方有一个“键 - os_version”,但我看不出它们之间的关系):
[xml]$xml = system_profiler SPSoftwareDataType -xml
$xml.plist.array.dict.array.dict.string -match 'macos'
macOS 10.12.6 (16G1510)
对于其他答案的评论中描述的问题,这将适用于任何版本的 Powershell。
$iswin = $PSVersionTable.Platform -match '^($|(Microsoft )?Win)'
$False
是'nix。
在 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 的评论; 我写了这个答案,因为这似乎是如何从评论中获得答案的。
如果您没有安装最新的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.