次の方法で共有


互換性のある 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」を参照してください。

互換性のあるエディションの宣言

モジュール作成者は、モジュール マニフェスト キーを使用して、モジュールを 1 つ以上の PowerShell エディションと互換性がある CompatiblePSEditions 宣言できます。 このキーは、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_Desktop タグと PSEdition_Core タグのないモジュールは、PowerShell Desktop エディションで正常に動作すると見なされます。

# 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 エディションのいずれかまたは両方の (Desktop と Core) を対象とする 1 つのモジュールを発行できます。

1 つのモジュールは Desktop エディションと Core エディションの両方で動作できますが、そのモジュール作成者は、変数を使用して RootModule またはモジュール マニフェストに必要なロジックを追加 $PSEdition 必要があります。 モジュールには、 CoreCLRFullCLR の両方を対象とする 2 セットのコンパイル済み 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