Otra mirada a la información de la versión de PowerShell Core
A medida que PowerShell Core comience a extenderse a nuestro mundo y comencemos a pensar en trabajar y crear secuencias de comandos multiplataforma, será útil saber en qué tipo de plataforma se está ejecutando. La $PSVersionTable incorporada es un lugar obvio para comenzar. En PowerShell Core también hay algunas variables integradas nuevas que puede usar.
Pero estas variables no están en Windows PowerShell. Así que decidí agregar otra herramienta a la mezcla: una función simple que brindará una vista instantánea de la información de la versión, incluidas las posibles opciones de comunicación remota disponibles. Debería ser WinRM para plataformas Windows y potencialmente SSH. La función no es especialmente complicada.
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)
}
}
Así es como se ve en una variedad de plataformas.
Podría empaquetar esto en mi módulo PSScriptTools.
¿Has empezado a adoptar este mundo multiplataforma? ¿Qué desafíos ha encontrado y cómo los ha superado? Me encantaría escuchar tus historias.