Seb*_*bMa 3 powershell uname platform-detection
我uname -s在 bash 脚本中使用来确定操作系统,当它在 Linux、macOS 或 Windows 上运行时,它会返回 Linux、Darwin 或 MINGW64_NT...。
EDIT0 :我希望我的$PROFILE脚本能够检测操作系统是否在带有 PS 的 Windows(版本可能低于 6)或带有 PSv>=6 的 Linux 上运行。
我在 powershell 中找到了这个:
PS> [System.Environment]::OSVersion.Platform
Run Code Online (Sandbox Code Playgroud)
在 Linux 上,它返回Unix,在 64 位 Windows 上,它返回Win32NT.
我没有可用的 macOS(还没有:)),所以我不知道它在 macOS 上实际返回什么。
UnixEDIT1:此方法在 Windows32b 和 Windows64b之间似乎没有区别Linux。
还有哪些其他方法可以检测 powershell 5.1 中的操作系统?
小智 6
问题是Powershell 5.1。
更多详细信息请参见:通过 Powershell 确定操作系统版本、Linux 和 Windows
Powershell 5.1 没有能力确定 Microsoft 环境之外的操作系统。你能做的最好的事情就是使用 get-wmi 或 cim-session
windows
!windows
Run Code Online (Sandbox Code Playgroud)
对于PowerShell Core(Powershell 版本 6.0+),您可以使用自动变量:
$IsLinux
$IsMacOS
$IsWindows
Run Code Online (Sandbox Code Playgroud)
使用 6+,您可以执行以下操作:
foreach ($i in $info) {
if ($i -eq $IsLinux) {
Write-Host $i is Linux
}
elseif ($i -eq $IsMacOS) {
Write-Host $i is This is a dirty, dirty Mac
}
elseif ($i -eq $IsWindows) {
Write-Host $i is Windows
}
}
Run Code Online (Sandbox Code Playgroud)
要结束这个问题,您所要求的工作根本不可能/可能不值得使用 PowerShell 5.1 来完成。
| 归档时间: |
|
| 查看次数: |
5436 次 |
| 最近记录: |