關於 PowerShell 版本

簡短描述

不同版本的 PowerShell 會在不同的基礎執行時間上執行。

完整描述

從 PowerShell 5.1,PowerShell 有多個版本,每個 版本 都會在不同的 .NET 執行時間上執行。 從 PowerShell 6.2 起,PowerShell 有兩個版本:

  • 桌面,可在 .NET Framework 上執行。 PowerShell 4 和以下版本,以及完整功能 Windows 版本上的 PowerShell 5.1,例如 Windows Desktop、Windows Server、Windows Server Core 和其他大部分 Windows 作業系統都是 Desktop 版本。 這是原始的 PowerShell 版本。
  • .NET Core 上執行的 Core。 PowerShell 6.0 和更新版本,以及某些使用量較低的 Windows 版本上的 PowerShell 5.1,例如無法使用.NET Framework的 Windows Nano Server 和 Windows IoT。

因為 PowerShell 版本對應至其 .NET 執行時間,所以它是 .NET API 和 PowerShell 模組相容性的主要指標;.NET 執行時間中無法使用某些 .NET API、類型或方法,這會影響相依于這些 API 的 PowerShell 腳本和模組。

$PSEdition自動變數

在 PowerShell 5.1 和更新版本中,您可以找出您使用自動變數執行 $PSEdition 的版本:

$PSEdition
Core

在 PowerShell 4 和以下版本中,此變數不存在。 $PSEdition 當為 null 時,應該視為與具有 值 Desktop 相同的 。

中版本 $PSVersionTable

自動 $PSVersionTable 變數也有 PowerShell 5.1 和更新版本中的版本資訊:

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.2.0-rc.1
PSEdition                      Core           # <-- Edition information
GitCommitId                    6.2.0-rc.1
OS                             Microsoft Windows 10.0.18865
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

欄位 PSEdition 的值會與 $PSEdition 自動變數相同。

模組 CompatiblePSEditions 資訊清單欄位

PowerShell 模組可以使用模組資訊清單的 欄位,宣告其與 CompatiblePSEditions 哪些 PowerShell 版本相容。

例如,宣告與 Desktop PowerShell 版本 Core 相容性的模組資訊清單:

@{
    ModuleVersion = '1.0'
    FunctionsToExport = @('Test-MyModule')
    CompatiblePSEditions = @('Desktop', 'Core')
}

Desktop 相容性的模組資訊清單範例:

@{
    ModuleVersion = '1.0'
    FunctionsToExport = @('Test-MyModule')
    CompatiblePSEditions = @('Desktop')
}

省略模組資訊清單中的 CompatiblePSEditions 欄位會與將它 Desktop 設定為 相同,因為在此欄位導入之前建立的模組是針對這個版本隱含撰寫的。

對於未隨附于 Windows (的模組,也就是您從資源庫撰寫或安裝的模組) ,此欄位僅供參考;PowerShell 不會根據 CompatiblePSEditions 欄位變更行為,但會針對您自己的邏輯在) 傳 Get-Module 回的物件 (上公開 PSModuleInfo 它:

New-ModuleManifest -Path .\TestModuleWithEdition.psd1 -CompatiblePSEditions Desktop,Core -PowerShellVersion '5.1'
$ModuleInfo = Test-ModuleManifest -Path .\TestModuleWithEdition.psd1
$ModuleInfo.CompatiblePSEditions
Desktop
Core

注意

模組 CompatiblePSEditions 欄位只與 PowerShell 5.1 和更新版本相容。 包含此欄位會導致模組與 PowerShell 4 和以下版本不相容。 由於欄位完全是參考的,因此可以在稍後的 PowerShell 版本中安全地省略。

在 PowerShell 6.1 中, Get-Module -ListAvailable 已更新其格式器以顯示每個模組的版本相容性:

Get-Module -ListAvailable

    Directory: C:\Users\me\Documents\PowerShell\Modules

ModuleType Version    Name                   PSEdition ExportedCommands
---------- -------    ----                   --------- ----------------
Script     1.4.0      Az                     Core,Desk
Script     1.3.1      Az.Accounts            Core,Desk {Disable-AzDataCollection, Disable-AzContextAutosave, E...
Script     1.0.1      Az.Aks                 Core,Desk {Get-AzAks, New-AzAks, Remove-AzAks, Import-AzAksCreden...

...

Script     4.4.0      Pester                 Desk      {Describe, Context, It, Should...}
Script     1.18.0     PSScriptAnalyzer       Desk      {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-...
Script     1.0.0      WindowsCompatibility   Core      {Initialize-WinSession, Add-WinFunction, Invoke-WinComm...

隨附于 Windows 的模組版本相容性

對於屬於 Windows (或安裝為角色或功能) 的模組,PowerShell 6.1 和更新版本會以不同的方式處理 CompatiblePSEditions 欄位。 這類別模組位於Windows PowerShell系統模組目錄中, (%windir%\System\WindowsPowerShell\v1.0\Modules) 。

針對從這個目錄中載入或找到的模組,PowerShell 6.1 和更新版本會使用 CompatiblePSEditions 欄位來判斷模組是否與目前的會話相容,並據以運作。

使用 時 Import-Module ,不會匯入不含 CoreCompatiblePSEditions 模組,而且會顯示錯誤:

Import-Module BitsTransfer
Import-Module : Module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsT
ransfer\BitsTransfer.psd1' does not support current PowerShell edition 'Core'.
Its supported editions are 'Desktop'. Use 'Import-Module -SkipEditionCheck' to i
gnore the compatibility of this module.
At line:1 char:1
+ Import-Module BitsTransfer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (C:\WINDOWS\system32\u2026r\BitsT
ransfer.psd1:String) [Import-Module], InvalidOperationException
+ FullyQualifiedErrorId : Modules_PSEditionNotSupported,Microsoft.PowerShell.Com
mands.ImportModuleCommand

使用 時 Get-Module -ListAvailable ,不會顯示不含 CoreCompatiblePSEditions 模組:

Get-Module -ListAvailable BitsTransfer
# No output

在這兩種情況下, -SkipEditionCheck switch 參數都可以用來覆寫此行為:

Get-Module -ListAvailable -SkipEditionCheck BitsTransfer
    Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

ModuleType Version    Name             PSEdition ExportedCommands
---------- -------    ----             --------- ----------------
Manifest   2.0.0.0    BitsTransfer     Desk      {Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer,...

警告

Import-Module -SkipEditionCheck 模組看起來可能成功,但使用該模組執行之後遇到不相容的風險;載入模組一開始成功時,命令稍後可能會呼叫不相容的 API 並失敗。

撰寫版本跨相容性的 PowerShell 模組

撰寫 PowerShell 模組以和 Core PowerShell 版本為目標 Desktop 時,您可以執行一些動作以確保跨版本相容性。

不過,確認並持續驗證相容性的唯一方法是撰寫腳本或模組的測試,並在您需要相容性的所有 PowerShell 版本上執行測試。 建議的測試架構是 Pester

PowerShell 指令碼

作為語言,PowerShell 在版本之間運作相同;它是受版本相容性影響的 Cmdlet、模組和 .NET API。

一般而言,在 PowerShell 6.1 和更新版本中運作的腳本會使用 Windows PowerShell 5.1,但有一些例外狀況。

1.18.0 版 PSScriptAnalyzer 模組具有 PSUseCompatibleCommandsPSUseCompatibleTypes 等規則,能夠偵測 PowerShell 腳本中命令和 .NET API 的可能不相容使用方式。

.NET 組件

如果您要撰寫二進位模組或包含 .NET 元件的模組, (DLL) 從原始程式碼產生,您應該針對 .NET StandardPowerShell Standard 進行編譯,以進行 .NET 和 PowerShell API 相容性的編譯時間相容性驗證。

雖然這些程式庫能夠在編譯時期檢查一些相容性,但無法攔截版本之間的可能行為差異。 為此,您仍必須撰寫測試。

另請參閱