Modul dengan Edisi PowerShell yang kompatibel
Dimulai dengan versi 5.1, PowerShell tersedia dalam edisi yang berbeda, yang menunjukkan berbagai set fitur dan kompatibilitas platform.
- Edisi Desktop: Dibangun di .NET Framework, berlaku untuk Windows PowerShell v4.0 ke bawah serta Windows PowerShell 5.1 di Windows Desktop, Windows Server, Windows Server Core dan sebagian besar edisi Windows lainnya.
- Edisi Inti: Dibangun di .NET Core, berlaku untuk PowerShell 6.0 ke atas serta Windows PowerShell 5.1 pada pengurangan jejak Edisi Windows seperti Windows IoT dan Windows Nano Server.
Untuk informasi selengkapnya tentang edisi PowerShell, lihat about_PowerShell_Editions.
Mendeklarasikan edisi yang kompatibel
Penulis modul dapat mendeklarasikan modulnya agar kompatibel dengan satu atau beberapa edisi PowerShell menggunakan CompatiblePSEditions
kunci manifes modul. Kunci ini hanya didukung pada PowerShell 5.1 atau yang lebih baru.
Catatan
Setelah manifes modul ditentukan dengan CompatiblePSEditions
kunci atau menggunakan $PSEdition
variabel , manifes modul tidak dapat diimpor pada PowerShell v4 atau yang lebih rendah.
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;}
Saat mendapatkan daftar modul yang tersedia, Anda dapat memfilter daftar menurut edisi 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
Dimulai di PowerShell 6, CompatiblePSEditions
nilai digunakan untuk memutuskan apakah modul kompatibel saat modul diimpor dari $env:windir\System32\WindowsPowerShell\v1.0\Modules
.
Perilaku ini hanya berlaku untuk Windows. Di luar skenario ini, nilai hanya digunakan sebagai metadata.
Menemukan modul yang kompatibel
Pengguna Galeri PowerShell dapat menemukan daftar modul yang didukung pada Edisi PowerShell tertentu menggunakan tag PSEdition_Desktop dan PSEdition_Core.
Modul tanpa PSEdition_Desktop dan tag PSEdition_Core dianggap berfungsi dengan baik pada edisi 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
Menargetkan beberapa edisi
Penulis modul dapat menerbitkan satu penargetan modul ke edisi PowerShell (Desktop dan Core).
Satu modul dapat berfungsi pada edisi Desktop dan Core, dalam penulis modul tersebut harus menambahkan logika yang diperlukan baik di RootModule atau dalam manifes modul menggunakan $PSEdition
variabel. Modul dapat memiliki dua set DLL yang dikompilasi yang menargetkan CoreCLR dan FullCLR. Berikut adalah opsi pengemasan dengan logika untuk memuat DLL yang tepat.
Opsi 1: Mengemas modul untuk menargetkan beberapa versi dan beberapa edisi PowerShell
Isi folder modul
- Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
- Microsoft.Windows.PowerShell.ScriptAnalyzer.dll
- PSScriptAnalyzer.psd1
- PSScriptAnalyzer.psm1
- ScriptAnalyzer.format.ps1xml
- ScriptAnalyzer.type.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
Isi file
@{
# 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'
# ---
}
Logika di bawah ini memuat rakitan yang diperlukan tergantung pada edisi atau versi saat ini.
PSScriptAnalyzer.psm1
Isi file:
#
# 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
}
Opsi 2: Gunakan variabel $PSEdition dalam file PSD1 untuk memuat DLL yang tepat
Di PS 5.1 atau yang lebih baru, $PSEdition
variabel global diizinkan dalam file manifes modul. Dengan menggunakan variabel ini, penulis modul dapat menentukan nilai bersyarah dalam file manifes modul. $PSEdition
variabel dapat direferensikan dalam mode bahasa terbatas atau bagian Data.
Contoh file manifes modul dengan CompatiblePSEditions
kunci.
@{
# 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'
}
}
Konten modul
- 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
Detail Selengkapnya
PowerShell Gallery
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk