Delen via


Modules met compatibele PowerShell-edities

Vanaf versie 5.1 is PowerShell beschikbaar in verschillende edities, die verschillende functiesets en platformcompatibiliteit aangeven.

  • Desktop-editie: Gebouwd op .NET Framework, is van toepassing op Windows PowerShell v4.0 en lager, evenals Windows PowerShell 5.1 op Windows Desktop, Windows Server, Windows Server Core en de meeste andere Windows-edities.
  • Kerneditie: Gebouwd op .NET Core, is van toepassing op PowerShell 6.0 en hoger, evenals Windows PowerShell 5.1 op Windows-edities met een kleinere voetafdruk, zoals Windows IoT en Windows Nano Server.

Zie about_PowerShell_Editions voor meer informatie over PowerShell-edities.

Compatibele edities declareren

Auteurs van modules kunnen verklaren dat hun modules compatibel zijn met een of meer PowerShell-edities met behulp van de CompatiblePSEditions modulemanifestsleutel. Deze sleutel wordt alleen ondersteund op PowerShell 5.1 of hoger.

Opmerking

Zodra een modulemanifest is opgegeven met de CompatiblePSEditions sleutel of de $PSEdition variabele gebruikt, kan deze niet worden geïmporteerd op PowerShell v4 of lager.

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

Wanneer u een lijst met beschikbare modules ophaalt, kunt u de lijst filteren op PowerShell-editie.

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

Vanaf PowerShell 6 wordt de CompatiblePSEditions waarde gebruikt om te bepalen of een module compatibel is wanneer modules worden geïmporteerd uit $env:windir\System32\WindowsPowerShell\v1.0\Modules. Dit gedrag is alleen van toepassing op Windows. Buiten dit scenario wordt de waarde alleen gebruikt als metagegevens.

Compatibele modules vinden

Gebruikers van PowerShell Gallery kunnen de lijst met modules vinden die worden ondersteund op een specifieke PowerShell-editie met behulp van tags PSEdition_Desktop en PSEdition_Core.

Modules zonder PSEdition_Desktop en PSEdition_Core-tags worden geacht goed te werken op PowerShell Desktop-edities.

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

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

Targeting op meerdere edities

Auteurs van modules kunnen één module publiceren die is gericht op een of beide PowerShell-edities (bureaublad en kern).

Een enkele module kan werken op zowel Desktop- als Core-edities, in die zin dat de auteur van de module de vereiste logica moet toevoegen in RootModule of in het modulemanifest met behulp van $PSEdition variabele. Modules kunnen twee sets gecompileerde DLL's hebben die gericht zijn op zowel CoreCLR als FullCLR. Hier zijn de verpakkingsopties met logica voor het laden van de juiste DLL's.

Optie 1: Een module verpakken voor het targeten van meerdere versies en meerdere edities van PowerShell

Inhoud van de modulemap

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

Inhoud van PSScriptAnalyzer.psd1 het dossier

@{

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

# ---
}

Onder de logica worden de benodigde assemblages geladen, afhankelijk van de huidige editie of versie.

Inhoud van PSScriptAnalyzer.psm1 het bestand:

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

Optie 2: Gebruik $PSEdition variabele in het PSD1-bestand om de juiste DLL's te laden

In PS 5.1 of nieuwer $PSEdition is een globale variabele toegestaan in het manifestbestand van de module. Met behulp van deze variabele kan de auteur van de module de voorwaardelijke waarden in het manifestbestand van de module opgeven. $PSEdition variabele kan worden verwezen in de beperkte taalmodus of in een gegevenssectie.

Voorbeeldmodule manifestbestand met CompatiblePSEditions sleutel.

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

Inhoud van de module

  • 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

Meer informatie

Scripts met PSEditions

PSEditions-ondersteuning op PowerShellGallery

Update module manifest

about_PowerShell_Editions