网站搜索

再看一下PowerShell核心版本信息


随着 PowerShell Core 开始传播到我们的世界,当我们开始考虑跨平台工作和编写脚本时,了解您正在运行的平台类型将很有用。内置的 $PSVersionTable 显然是一个起点。在 PowerShell Core 上,您还可以使用一些新的内置变量。

但这些变量不在 Windows PowerShell 上。因此,我决定添加另一个工具 - 一个简单的功能,它将提供版本信息的快照,包括可用的远程处理选项。这应该是适用于 Windows 平台的 WinRM,也可能是 SSH。该功能并不是特别复杂。

Function Get-PSVersion {
<#
.Synopsis
Get a short view of PowerShell version information

.Description
Use this command to get a quick snapshot of PowerShell version information. The
Remoting property should indicate what remoting capabilities are potentially
available.

.Example
PS C:\> Get-PSVersion

Version      : 5.1.16299.98
Edition      : Desktop
Platform     : Windows
Remoting     : {WinRM}
Computername : Win10Desk

Running on a Windows 10 desktop.

.Example
PS C:\> Get-PSVersion


Version      : 6.0.1
Edition      : Core
Platform     : Win32NT
Remoting     : {WinRM, SSH}
Computername : SRV1

Running PowerShell Core on a Windows platform with SSH installed.

.Example
PS /mnt/c/scripts> get-psversion


Version      : 6.0.1
Edition      : Core
Platform     : Unix
Remoting     : {SSH}
Computername : Bovine320

PowerShell Core running in a Linux environment.

.Link
$PSVersionTable


#>
[cmdletbinding()]
Param()

$ver = $PSVersionTable.PSVersion
$edition = $PSVersionTable.PSEdition

if ($PSVersionTable.platform) {
    $platform = $PSVersionTable.platform
}
else {
    $platform = "Windows"
}

$remoting = @()
if (Test-Path 'WSMan:') {
    $remoting+="WinRM"
}
if (Get-Command ssh -ErrorAction SilentlyContinue) {
    $remoting+="SSH"
}

#use the hostname command because non-windows systems won't have
#$env:computername.
[pscustomobject]@{
    Version = $ver
    Edition = $edition
    Platform = $platform
    Remoting = $Remoting
    Computername = $(hostname)
}

}

这是它在各种平台上的样子。

我可能会将其打包到我的 PSScriptTools 模块中。

您已经开始拥抱这个跨平台的世界了吗?您遇到过哪些挑战以及如何克服这些挑战?我很想听听你的故事。

相关文章