从 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 PowerShell 5.1 在占用空间较小的 Windows 版本(如 Windows IoT 和 Windows Nano Server)上。
有关 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 开始,当从 $env:windir\System32\WindowsPowerShell\v1.0\Modules导入模块时,该CompatiblePSEditions值用于确定模块是否兼容。
此行为仅适用于 Windows。 在此方案之外,该值仅用作元数据。
查找兼容模块
PowerShell 库用户可以使用标记 PSEdition_Desktop 和 PSEdition_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 版本(桌面版和核心版)的单个模块。
单个模块可以在桌面版和核心版上运行,在该模块中,作者必须使用 variable 在 $PSEdition RootModule 或模块清单中添加所需的逻辑。 模块可以有两组面向 CoreCLR 和 FullCLR 的已编译 DLL。 下面是带有加载适当 DLL 的逻辑的打包选项。
选项 1:打包模块以面向多个版本和多个版本的 PowerShell
模块文件夹内容
- Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
- Microsoft.Windows.PowerShell.ScriptAnalyzer.dll
- PSScriptAnalyzer.psd1
- PSScriptAnalyzer.psm1
- 脚本分析器.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