共用方式為


關於 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版本。
  • Core,可在 .NET Core 上執行。 PowerShell 6.0 和更新版本,以及 Windows Nano Server 和 Windows IoT 等 Windows 版本上的 PowerShell 5.1,其中 .NET Framework 無法使用。

因為 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 和更新版本中也有版本資訊:

$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 ,將不會顯示不含 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 搭配運作,但有一些例外狀況。

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

.NET 組件

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

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

另請參閱