Kompatibilis PowerShell-kiadásokkal rendelkező modulok

Az 5.1-es verziótól kezdve a PowerShell különböző kiadásokban érhető el, amelyek különböző funkciókészleteket és platformkompatibilitást jelölnek.

  • Asztali kiadás: A .NET-keretrendszer-ra épülő Windows PowerShell 4.0-s és újabb verziójára, valamint Windows PowerShell 5.1-et a Windows Desktop, a Windows Server, a Windows Server Core és a legtöbb egyéb Windows-kiadás esetében.
  • Core Edition: A .NET Core-ra épül, a PowerShell 6.0-s és újabb verziójára, valamint Windows PowerShell 5.1-re a csökkentett kihasználtságú Windows-kiadásokon, például a Windows IoT-n és a Windows Nano Serveren.

További információ a PowerShell-kiadásokról: about_PowerShell_Editions.

Kompatibilis kiadások deklarálása

A modulszerzők a moduljegyzék-kulccsal deklarálhatják a moduljaikat egy vagy több PowerShell-kiadással CompatiblePSEditions kompatibilisnek. Ez a kulcs csak a PowerShell 5.1-es vagy újabb verzióin támogatott.

Megjegyzés

Ha egy moduljegyzék meg van adva a CompatiblePSEditions kulccsal, vagy a $PSEdition változót használja, nem importálható a PowerShell 4-ben vagy annál alacsonyabb verzióban.

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

Az elérhető modulok listáját szűrheti a PowerShell-kiadás alapján.

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

A PowerShell 6-tól kezdődően a CompatiblePSEditions rendszer azt határozza meg, hogy egy modul kompatibilis-e a modul importálásával $env:windir\System32\WindowsPowerShell\v1.0\Modules. Ez a viselkedés csak a Windowsra vonatkozik. Ezen a forgatókönyven kívül az érték csak metaadatként használatos.

Kompatibilis modulok keresése

PowerShell-galéria felhasználók az adott PowerShell-kiadásban támogatott modulok listáját PSEdition_Desktop ésPSEdition_Core címkékkel találják meg.

A PSEdition_Desktop és PSEdition_Core címkék nélküli modulok jól működnek a PowerShell Desktop-kiadásokban.

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

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

Több kiadás megcélzása

A modulszerzők egyetlen modulcélzást tehetnek közzé a PowerShell-kiadásokban (Desktop és Core).

Egyetlen modul az Asztali és a Core kiadásban is használható, ebben a modulban a szerzőnek kötelező logikát kell hozzáadnia a RootModule-ban vagy a moduljegyzékben változó használatával $PSEdition . A modulok két lefordított DLL-készlettel rendelkezhetnek, amelyek a CoreCLR-t és a FullCLR-t is célba kapják. Íme a csomagolási lehetőségek a megfelelő DLL-ek betöltésének logikájával.

1. lehetőség: Modul csomagolása a PowerShell több verziójának és több kiadásának megcélzására

A modulmappa tartalma

  • 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

PSScriptAnalyzer.psd1 A fájl tartalma

@{

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

# ---
}

Az alábbi logika betölti a szükséges szerelvényeket az aktuális kiadástól vagy verziótól függően.

PSScriptAnalyzer.psm1 A fájl tartalma:

#
# 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. lehetőség: Használja $PSEdition változót a PSD1 fájlban a megfelelő DLL-ek betöltéséhez

A PS 5.1-ben vagy újabb $PSEdition verzióban a globális változó engedélyezve van a modul jegyzékfájljában. Ezzel a változóval a modul szerzője megadhatja a feltételes értékeket a modul jegyzékfájljában. $PSEdition a változó korlátozott nyelvi módban vagy egy Adatszakaszban hivatkozhat gombra.

Mintamodul jegyzékfájlja kulccsal 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'
    }
}

A modul tartalma

  • 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

További részletek

PSEditions paraméterrel rendelkező parancsfájlok

PSEditions-támogatás a PowerShellGalleryben

Moduljegyzék frissítése

about_PowerShell_Editions