ดูข้อมูลเวอร์ชัน PowerShell Core อีกครั้ง
เมื่อ PowerShell Core เริ่มแพร่กระจายไปในโลกของเรา และเมื่อเราเริ่มคิดถึงการทำงานและการเขียนสคริปต์ข้ามแพลตฟอร์ม การทราบว่าคุณใช้งานแพลตฟอร์มประเภทใดจะเป็นประโยชน์ $PSVersionTable ในตัวเป็นจุดเริ่มต้นที่ชัดเจน บน PowerShell Core ยังมีตัวแปรในตัวใหม่ๆ บางตัวที่คุณสามารถใช้ได้
แต่ตัวแปรเหล่านี้ไม่ได้อยู่ใน Windows PowerShell ดังนั้นฉันจึงตัดสินใจเพิ่มเครื่องมืออื่นในการมิกซ์ ซึ่งเป็นฟังก์ชันง่ายๆ ที่จะให้ข้อมูลภาพรวมของข้อมูลเวอร์ชัน รวมถึงตัวเลือกระยะไกลที่เป็นไปได้ด้วย นี่ควรเป็น WinRM สำหรับแพลตฟอร์ม Windows และอาจเป็น 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 ของฉัน
คุณได้เริ่มยอมรับโลกข้ามแพลตฟอร์มนี้แล้วหรือยัง? คุณเคยเผชิญกับความท้าทายอะไรบ้าง และคุณเอาชนะมันได้อย่างไร? ฉันชอบที่จะได้ยินเรื่องราวของคุณ