Dela via


Moduler med kompatibla PowerShell-utgåvor

Från och med version 5.1 är PowerShell tillgängligt i olika utgåvor, vilket anger olika funktionsuppsättningar och plattformskompatibilitet.

  • Desktop Edition: Bygger på .NET Framework och gäller för Windows PowerShell v4.0 och tidigare samt Windows PowerShell 5.1 på Windows Desktop, Windows Server, Windows Server Core och de flesta andra Windows-utgåvor.
  • Core-utgåvan: Bygger på .NET Core och gäller för PowerShell 6.0 och senare samt Windows PowerShell 5.1 på Windows-utgåvor med minskat fotavtryck, till exempel Windows IoT och Windows Nano Server.

Mer information om PowerShell-utgåvor finns i about_PowerShell_Editions.

Deklarera kompatibla utgåvor

Modulförfattare kan deklarera att deras moduler är kompatibla med en eller flera PowerShell-utgåvor med hjälp av modulmanifestnyckeln CompatiblePSEditions . Den här nyckeln stöds endast i PowerShell 5.1 eller senare.

Anmärkning

När ett modulmanifest har angetts med CompatiblePSEditions nyckeln eller använder variabeln $PSEdition kan det inte importeras på PowerShell v4 eller lägre.

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;}

När du hämtar en lista över tillgängliga moduler kan du filtrera listan efter PowerShell-utgåva.

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

Från och med CompatiblePSEditions PowerShell 6 används värdet för att avgöra om en modul är kompatibel när moduler importeras från $env:windir\System32\WindowsPowerShell\v1.0\Modules. Det här beteendet gäller endast för Windows. Utanför det här scenariot används värdet endast som metadata.

Hitta kompatibla moduler

PowerShell-galleriet användare kan hitta listan över moduler som stöds i en specifik PowerShell-utgåva med hjälp av taggar PSEdition_Desktop och PSEdition_Core.

Moduler utan PSEdition_Desktop - och PSEdition_Core-taggar anses fungera bra i PowerShell Desktop-versioner.

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

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

Inriktning på flera utgåvor

Modulförfattare kan publicera en enskild modul som riktar sig till någon eller båda PowerShell-utgåvorna (Desktop och Core).

En enda modul kan fungera på både Desktop- och Core-utgåvorna, i det att modulförfattaren måste lägga till nödvändig logik i antingen RootModule eller i modulmanifestet med hjälp av $PSEdition variabeln. Moduler kan ha två uppsättningar kompilerade DLL:er som riktar sig mot både CoreCLR och FullCLR. Här är paketeringsalternativen med logik för att läsa in rätt DLL:er.

Alternativ 1: Paketera en modul för att rikta in sig på flera versioner och flera utgåvor av PowerShell

Innehåll i modulmapp

  • 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
  • Inställningar\CmdletDesign.psd1
  • Inställningar\DSC.psd1
  • Inställningar\ScriptFunctions.psd1
  • Inställningar\ScriptingStyle.psd1
  • Inställningar\ScriptSecurity.psd1

Filens PSScriptAnalyzer.psd1 innehåll

@{

# 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'

# ---
}

Nedanstående logik läser in de sammansättningar som krävs beroende på den aktuella utgåvan eller versionen.

Filens PSScriptAnalyzer.psm1 innehåll:

#
# 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
}

Alternativ 2: Använd $PSEdition variabel i PSD1-filen för att läsa in rätt DLL:er

I PS 5.1 eller senare $PSEdition tillåts globala variabler i modulmanifestfilen. Med hjälp av den här variabeln kan modulförfattaren ange villkorsvärden i modulmanifestfilen. $PSEdition variabeln kan refereras till i begränsat språkläge eller i ett dataavsnitt.

Exempel på manifestfil för modul med CompatiblePSEditions nyckel.

@{
    # 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'
    }
}

Modulens innehåll

  • 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

Mer information

Skript med PSEditions

Stöd för PSEditions på PowerShellGallery

Manifest för uppdateringsmodul

about_PowerShell_Editions