共用方式為


具有相容 PowerShell 版本的模組

從 5.1 版開始,PowerShell 有不同的版本,這表示不同的功能集和平台相容性。

  • 桌面版: 以 .NET Framework 為基礎,適用於 Windows PowerShell v4.0 及更低版本,以及 Windows 桌面、Windows Server、Windows Server Core 和大多數其他 Windows 版本上的 Windows PowerShell 5.1。
  • 核心版: 以 .NET Core 為基礎,適用於 PowerShell 6.0 和更新版本,以及 Windows IoT 和 Windows Nano Server 等減少佔用空間的 Windows 版本上的 Windows PowerShell 5.1。

如需 PowerShell 版本的詳細資訊,請參閱 about_PowerShell_Editions

宣告相容版本

模組作者可以使用模組資訊清單索引鍵, CompatiblePSEditions 宣告其模組與一或多個 PowerShell 版本相容。 此金鑰僅支援 PowerShell 5.1 或更新版本。

備註

使用索引鍵指定 CompatiblePSEditions 模組資訊清單或使用 $PSEdition 變數之後,就無法在 PowerShell v4 或更低版本上匯入它。

New-ModuleManifest -Path .\TestModuleWithEdition.psd1 -CompatiblePSEditions Desktop,Core -PowerShellVersion 5.1
$ModuleInfo = Test-ModuleManifest -Path .\TestModuleWithEdition.psd1
$ModuleInfo.CompatiblePSEditions
Desktop
Core
$ModuleInfo | Get-Member CompatiblePSEditions
   TypeName: System.Management.Automation.PSModuleInfo

Name                 MemberType Definition
----                 ---------- ----------
CompatiblePSEditions Property   System.Collections.Generic.IEnumerable[string] CompatiblePSEditions {get;}

取得可用模組清單時,您可以依 PowerShell 版本篩選清單。

Get-Module -ListAvailable -PSEdition Desktop
    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0        ModuleWithPSEditions
Get-Module -ListAvailable -PSEdition Core | % CompatiblePSEditions
Desktop
Core

從 PowerShell 6 開始,當模組從 $env:windir\System32\WindowsPowerShell\v1.0\Modules匯入時,會用來CompatiblePSEditions決定模組是否相容。 此行為僅適用於 Windows。 在此案例之外,此值只會用作中繼資料。

尋找相容的模組

PowerShell 資源庫使用者可以使用標籤 PSEdition_DesktopPSEdition_Core 來尋找特定 PowerShell 版本支援的模組清單。

沒有 PSEdition_DesktopPSEdition_Core 標籤的模組會被視為在 PowerShell 桌面版本上正常運作。

# Find modules supported on PowerShell Desktop edition
Find-Module -Tag PSEdition_Desktop

# Find modules supported on PowerShell Core editions
Find-Module -Tag PSEdition_Core

以多個版本為目標

模組作者可以將單一模組發佈為目標至其中一個或兩個 PowerShell 版本 (桌面版和核心版) 。

單一模組可以在桌面版和核心版上運作,因為該模組作者必須在RootModule或模組資訊清單中使用變數新增 $PSEdition 所需的邏輯。 模組可以有兩組以 CoreCLRFullCLR 為目標的已編譯 DLL。 以下是包含載入適當 DLL 邏輯的封裝選項。

選項 1:封裝模組以多個版本和多個版本的 PowerShell 為目標

模組資料夾內容

  • Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
  • Microsoft.Windows.PowerShell.ScriptAnalyzer.dll
  • PSScriptAnalyzer.psd1
  • PSScriptAnalyzer.psm1
  • ScriptAnalyzer.format.ps1xml
  • ScriptAnalyzer.types.ps1xml
  • coreclr\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
  • coreclr\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll
  • en-US\about_PSScriptAnalyzer.help.txt
  • en-US\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml
  • PSv3\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
  • PSv3\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll
  • 設定\CmdletDesign.psd1
  • 設定\DSC.psd1
  • 設定\ScriptFunctions.psd1
  • 設定\ScriptingStyle.psd1
  • 設定\ScriptSecurity.psd1

檔案內容PSScriptAnalyzer.psd1

@{

# Author of this module
Author = 'Microsoft Corporation'

# Script module or binary module file associated with this manifest.
RootModule = 'PSScriptAnalyzer.psm1'

# Version number of this module.
ModuleVersion = '1.6.1'

# ---
}

以下邏輯根據當前版本或版本載入所需的組件。

檔案內容 PSScriptAnalyzer.psm1

#
# Script module for module 'PSScriptAnalyzer'
#
Set-StrictMode -Version Latest

# Set up some helper variables to make it easier to work with the module
$PSModule = $ExecutionContext.SessionState.Module
$PSModuleRoot = $PSModule.ModuleBase

# Import the appropriate nested binary module based on the current PowerShell version
$binaryModuleRoot = $PSModuleRoot


if (($PSVersionTable.Keys -contains "PSEdition") -and ($PSVersionTable.PSEdition -ne 'Desktop')) {
    $binaryModuleRoot = Join-Path -Path $PSModuleRoot -ChildPath 'coreclr'
}
else
{
    if ($PSVersionTable.PSVersion -lt [Version]'5.0')
    {
        $binaryModuleRoot = Join-Path -Path $PSModuleRoot -ChildPath 'PSv3'
    }
}

$binaryModulePath = Join-Path -Path $binaryModuleRoot -ChildPath 'Microsoft.Windows.PowerShell.ScriptAnalyzer.dll'
$binaryModule = Import-Module -Name $binaryModulePath -PassThru

# When the module is unloaded, remove the nested binary module that was loaded with it
$PSModule.OnRemove = {
    Remove-Module -ModuleInfo $binaryModule
}

選項 2:使用 PSD1 檔案中的$PSEdition變數載入適當的 DLL

在 PS 5.1 或更新版本中, $PSEdition 模組資訊清單檔案中允許使用全域變數。 使用此變數,模組作者可以在模組資訊清單檔案中指定條件值。 $PSEdition 變數可以在受限語言模式或資料區段中參照。

具有金鑰的 CompatiblePSEditions 範例模組資訊清單檔案。

@{
    # Script module or binary module file associated with this manifest.
    RootModule = if($PSEdition -eq 'Core')
    {
        'coreclr\MyCoreClrRM.dll'
    }
    else # Desktop
    {
        'clr\MyFullClrRM.dll'
    }

    # Supported PSEditions
    CompatiblePSEditions = 'Desktop', 'Core'

    # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
    NestedModules = if($PSEdition -eq 'Core')
    {
        'coreclr\MyCoreClrNM1.dll',
        'coreclr\MyCoreClrNM2.dll'
    }
    else # Desktop
    {
        'clr\MyFullClrNM1.dll',
        'clr\MyFullClrNM2.dll'
    }
}

模組內容

  • ModuleWithEditions\ModuleWithEditions.psd1
  • ModuleWithEditions\clr\MyFullClrNM1.dll
  • ModuleWithEditions\clr\MyFullClrNM2.dll
  • ModuleWithEditions\clr\MyFullClrRM.dll
  • ModuleWithEditions\coreclr\MyCoreClrNM1.dll
  • ModuleWithEditions\coreclr\MyCoreClrNM2.dll
  • ModuleWithEditions\coreclr\MyCoreClrRM.dll

更多詳細數據

具有 PSEditions 的腳本

PowerShellGallery 上的 PSEditions 支援

更新模組資訊清單

about_PowerShell_Editions