다음을 통해 공유


호환되는 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를 참조하세요.

호환 가능한 버전 선언

모듈 작성자는 모듈 매니페스트 키를 사용하여 CompatiblePSEditions 하나 이상의 PowerShell 버전과 호환되도록 모듈을 선언할 수 있습니다. 이 키는 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 CompatiblePSEditions 부터 이 값은 모듈을 가져올 $env:windir\System32\WindowsPowerShell\v1.0\Modules때 모듈이 호환되는지 여부를 결정하는 데 사용됩니다. 이 동작은 Windows에만 적용됩니다. 이 시나리오 외부에서는 값이 메타데이터로만 사용됩니다.

호환 가능한 모듈 찾기

PowerShell 갤러리 사용자는 PSEdition_DesktopPSEdition_Core 태그를 사용하여 특정 PowerShell 버전에서 지원되는 모듈 목록을 찾을 수 있습니다.

PSEdition_DesktopPSEdition_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 버전 모두에서 작동할 수 있으며, 모듈 작성자는 변수를 사용하여 $PSEdition RootModule 또는 모듈 매니페스트에 필요한 논리를 추가해야 합니다. 모듈에는 CoreCLRFullCLR을 모두 대상으로 하는 두 개의 컴파일된 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