about_PowerShell_Editions
簡短描述
不同版本的 PowerShell 在不同的基礎執行時間上執行。
完整描述
從 PowerShell 5.1,PowerShell 有多個版本,每個 版本 都會在不同的 .NET 執行時間上執行。 從 PowerShell 6.0 起,PowerShell 有兩個版本:
- 桌面,可在.NET Framework上執行。 PowerShell 4 和以下版本以及 PowerShell 5.1 適用于功能完整的 Windows 版本,例如 Windows Desktop、Windows Server、Windows Server Core 和其他大部分的 Windows 作業系統。 這是原始的 PowerShell 版本,並包含在作業系統的預設安裝中。
- Core,可在 .NET Core 上執行。 PowerShell 6.0 和更新版本會與功能完整的 Windows 版本、Windows Nano Server 和 Windows IoT 等一些縮減的 Windows 版本,或 Linux 和 macOS 等非 Windows 平臺上並存安裝 PowerShell 6.0 和更新版本。
因為 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.0-preview.3
PSEdition Core
GitCommitId 7.3.0-preview.3
OS Microsoft Windows 10.0.22000
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
的 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 版以上具有 PSUseCompatibleCommands 和 PSUseCompatibleTypes 等規則,可偵測 PowerShell 腳本中命令和 .NET API 可能不相容的使用情形。
.NET 組件
如果您要撰寫二進位模組或包含 .NET 元件的模組, (DLL) 從原始程式碼產生,您應該針對 .NET Standard 和 PowerShell Standard 進行編譯,以進行 .NET 和 PowerShell API 相容性的編譯時期相容性驗證。
雖然這些程式庫能夠在編譯時期檢查某些相容性,但無法攔截版本之間的可能行為差異。 為此,您仍然必須撰寫測試。