Modules met compatibele PowerShell-edities

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

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

Zie about_PowerShell_Editions voor meer informatie over PowerShell-edities.

Compatibele edities declareren

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

Notitie

Zodra een modulemanifest is opgegeven met de CompatiblePSEditions sleutel of de $PSEdition variabele gebruikt, kan het niet worden geïmporteerd in 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;}

Bij het ophalen van een lijst met beschikbare modules 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 zoeken

PowerShell Gallery gebruikers kunnen de lijst met modules vinden die worden ondersteund in een specifieke PowerShell Edition met behulp van tags PSEdition_Desktop en PSEdition_Core.

Modules zonder PSEdition_Desktop en PSEdition_Core tags werken goed 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

Gericht op meerdere edities

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

Eén module kan werken op zowel desktop- als core-edities. In die module moet de auteur de vereiste logica toevoegen in RootModule of in het modulemanifest met behulp van $PSEdition variabele. Modules kunnen twee sets gecompileerde DLL's hebben die zijn gericht op zowel CoreCLR als FullCLR. Dit zijn de verpakkingsopties met logica voor het laden van de juiste DLL's.

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

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

Inhoud van PSScriptAnalyzer.psd1 bestand

@{

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

# ---
}

De onderstaande logica laadt de vereiste assembly's, afhankelijk van de huidige editie of versie.

Inhoud van PSScriptAnalyzer.psm1 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 hoger $PSEdition is globale variabele toegestaan in het manifestbestand van de module. Met deze variabele kan de auteur van de module de voorwaardelijke waarden opgeven in het manifestbestand van de module. $PSEdition naar de variabele kan worden verwezen in de beperkte taalmodus of een sectie Gegevens.

Voorbeeldmodulemanifestbestand 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'
    }
}

Module-inhoud

  • 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

Ondersteuning voor PSEditions in PowerShellGallery

Modulemanifest bijwerken

about_PowerShell_Editions