about_PowerShell_Editions

簡短描述

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

詳細描述

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

  • 在 .NET Framework 上執行的桌面。 PowerShell 4 和更新版本以及 PowerShell 5.1 適用於 Windows Desktop、Windows Server、Windows Server Core 和其他大部分其他 Windows 操作系統等功能完整的 Windows 版本。 這是原始的PowerShell版本,並包含在作業系統的預設安裝中。
  • Core,可在 .NET Core 上執行。 PowerShell 6.0 和更新版本會與舊版 PowerShell 並存安裝在功能完整的 Windows 版本、一些縮減使用量的 Windows 版本,例如 Windows Nano Server 和 Windows IoT,或安裝在 Linux 和 macOS 等非 Windows 平臺上。

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

$PSEdition自動變數

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

$PSEdition
Core

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

中版本 $PSVersionTable

自動 $PSVersionTable 變數在 PowerShell 5.1 和更新版本中也有 PSEdition 屬性:

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.9
PSEdition                      Core
GitCommitId                    7.3.9
OS                             Microsoft Windows 10.0.22621
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 字段變更行為,但會針對您自己的邏輯在 PSModuleInfo 對象上 Get-Module公開它:。

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 ,不會匯入沒有 Core in CompatiblePSEditions 的模組,而且會顯示錯誤:

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

使用 時 Get-Module -ListAvailable ,將不會顯示沒有 Core in CompatiblePSEditions 的模組:

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 搭配運作,但有一些例外狀況。

PSScriptAnalyzer 1.18 版以上的規則具有 PSUseCompatibleCommandsPSUseCompatibleTypes 等規則,這些規則能夠偵測 PowerShell 腳本中命令和 .NET API 的可能不相容用法。

.NET 組件

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

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

另請參閱