问题描述
如何在脚本中使用Powershell确定操作系统类型(Linux,Windows)?
How can I determine the OS type, (Linux, Windows) using Powershell from within a script?
当我的脚本的这一部分在Linux主机上运行时,ResponseUri无法识别.
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
所以我想要一个 If
语句来确定看起来与此类似的操作系统类型:
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
我可以使用 Get-CimInstance Win32_OperatingSystem
,但是由于无法识别,它在Linux上将失败.
I could use Get-CimInstance Win32_OperatingSystem
but it would fail on Linux as it's not recognised.
推荐答案
您是否可以在其他平台上查看操作系统的环境变量?
Aren't there environment variables you can view on the other platforms for the OS?
Get-ChildItem -Path Env:
尤其是,至少在Windows上,存在一个OS环境变量,因此您应该可以通过使用 $ Env:OS
来完成此操作.
Particularly, on Windows at least, there's an OS environment variable, so you should be able to accomplish this by using $Env:OS
.
由于已经过去了一段时间,并且 PowerShell Core (v6)产品现已成为GA(从v7起, Core 品牌已被删除),您可以更准确地确定您的平台基于以下自动布尔变量:
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
这篇关于从Powershell确定操作系统版本,Linux和Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!