Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Powershell确定操作系统版本、Linux和Windows_Linux_Windows_Powershell_Operating System_Powershell Core - Fatal编程技术网

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

从Powershell确定操作系统版本、Linux和Windows,linux,windows,powershell,operating-system,powershell-core,Linux,Windows,Powershell,Operating System,Powershell Core,如何在脚本中使用Powershell确定操作系统类型(Linux、Windows) 在Linux主机上运行脚本的这一部分时,无法识别ResponseUri $UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority 因此,我需要一个If语句来确定与以下类似的操作系统类型: If ($OsType -eq "L

如何在脚本中使用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

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

在操作系统的其他平台上难道没有可以查看的环境变量吗

Get-ChildItem -Path Env:
特别是,至少在Windows上,有一个OS环境变量,因此您应该能够通过使用
$Env:OS
来实现这一点


由于一段时间过去了,而且PowerShell Core(v6)产品现在是GA(Core品牌从v7起已取消),因此您可以根据以下自动布尔变量更准确地确定您的平台:

$IsMacOS
$IsLinux
$IsWindows

当您只需检查它是windows还是linux时,也许您可以使用此选项(快速且肮脏):


由于上的PowerShell 6.1版已进入GA,因此您可以使用
$PSVersionTable
OS
平台和
GitCommitId

更新在v6.0.0-beta.3中有一些突破性的更改

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

$pVersionTable
关于:

平台
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
平台
Unix
OS
Linux(ubuntu)

平台
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控制台本身应该添加一些全局变量——但它们不被视为环境变量,这就是为什么在使用
dir env:
获取列表时它们不会出现的原因。我现在看到的操作系统特定变量是
$IsLinux
IsMacOS
$IsWindows
。这至少是适用于Mac/Linux的PowerShell 6.0.0-rc及以上版本

您可以只使用
Get Variable
查看可用内容的列表(在新会话中,如果您只需要默认的内置内容,则无需加载您的配置文件)。

对于(Powershell版本6.0+),您可以使用:
$IsLinux
$IsMacOS
$IsWindows

比如说,

if($IsLinux){
写主机“Linux”
}
elseif($IsMacOS){
写入主机“macOS”
}
elseif($IsWindows){
写入主机“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
或者(上面有一个“key-os_版本”,但我看不出它们之间有什么关联):


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

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

如果
$False
是'nix.

那么
get host
不包含该数据吗?我看不出来。也不是从我最初尝试的文档中,它在我的Windows 8.1机器上返回了“Windows\u NT”。。。我假设OP是在关注更多细节。@gms0ulman它看起来像是在任何Windows框上报告Windows\n。我认为OP是在寻找一个而不是另一个,因为他没有问“我在Windows 10或Ubuntu上吗?”@boomcubist我想到了一个更简单的检查(并且无法编辑我的其他评论):
If($env:OS)
,因为它只有两种状态suse
[System.Environment]::OSVersion.Platform
。它记录在<代码>$PSVersionTable.Platform。在Windows上是
Win32NT
,在Linux和macOS上是
Unix
。@DaveF因为PSv6已经上市了,现在有
$IsMacOS
$IsLinux
自动变量可用于确定您的操作系统。在版本6之前的PowerShell上运行时,这不会有任何帮助。@lit如果
$env:OS
不存在,则会显示一些不同的信息,如
$PSVersionTable.OS
$PSVersionTable.Platform
@LotPings-是的,可以从一些广泛的现有和不存在的事物集合中解读平台。听起来写起来像是喜怒无常的代码,维护起来更像喜怒无常。@lit我相信
[System.Environment]::OSVersion.Platform
适用于所有版本。请参阅我的评论,以获得公认的答案。@lit,看来我们无论如何都必须反向工作。。。代码试图假定它是最新和最好的,并向后工作到某种程度“我不能在这个环境中工作。请升级”。在Windows上,$PSVersionTable不在Powershell 1.0中。因此,尽管很糟糕,检查$PSVersionTable是否存在对于Powershell 1.0来说是一种廉价而肮脏的检查,它并没有像$PSVersionTable现在提供的那样提供一种方便的检查方法。
sw_vers -productVersion

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

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