Cara menulis manifes modul PowerShell

Setelah menulis modul PowerShell, Anda dapat menambahkan manifes modul opsional yang menyertakan informasi tentang modul. Misalnya, Anda dapat menjelaskan penulis, menentukan file dalam modul (seperti modul berlapis), menjalankan skrip untuk menyesuaikan lingkungan pengguna, memuat jenis dan memformat file, menentukan persyaratan sistem, dan membatasi anggota yang diekspor modul.

Membuat manifes modul

Manifes modul adalah file data PowerShell (.psd1) yang menjelaskan konten modul dan menentukan bagaimana modul diproses. File manifes adalah file teks yang berisi tabel hash kunci dan nilai. Anda menautkan file manifes ke modul dengan menamai manifes yang sama dengan modul, dan menyimpan manifes di direktori akar modul.

Untuk modul sederhana yang hanya berisi satu .psm1 atau rakitan biner, manifes modul bersifat opsional. Namun, rekomendasinya adalah menggunakan manifes modul jika memungkinkan, karena berguna untuk membantu Anda mengatur kode dan mempertahankan informasi penerapan versi. Dan, manifes modul diperlukan untuk mengekspor assembly yang diinstal di Global Assembly Cache. Manifes modul juga diperlukan untuk modul yang mendukung fitur Bantuan yang Dapat Diperbarui. Bantuan yang Dapat Diperbarui menggunakan kunci HelpInfoUri dalam manifes modul untuk menemukan file Informasi bantuan (HelpInfo XML) yang berisi lokasi file bantuan yang diperbarui untuk modul. Untuk informasi selengkapnya tentang Bantuan yang Dapat Diperbarui, lihat Mendukung Bantuan yang Dapat Diperbarui.

Untuk membuat dan menggunakan manifes modul

  1. Praktik terbaik untuk membuat manifes modul adalah menggunakan cmdlet New-ModuleManifest . Anda dapat menggunakan parameter untuk menentukan satu atau beberapa kunci dan nilai default manifes. Satu-satunya persyaratan adalah memberi nama file. New-ModuleManifest membuat manifes modul dengan nilai yang Anda tentukan, dan menyertakan kunci yang tersisa dan nilai defaultnya. Jika Anda perlu membuat beberapa modul, gunakan New-ModuleManifest untuk membuat templat manifes modul yang dapat dimodifikasi untuk berbagai modul Anda. Untuk contoh manifes modul default, lihat manifes Modul sampel.

    New-ModuleManifest -Path C:\myModuleName.psd1 -ModuleVersion "2.0" -Author "YourNameHere"

    Alternatifnya adalah membuat tabel hash manifes modul secara manual menggunakan informasi minimal yang diperlukan, ModuleVersion. Anda menyimpan file dengan nama yang sama dengan modul Anda dan menggunakan .psd1 ekstensi file. Anda kemudian dapat mengedit file dan menambahkan kunci dan nilai yang sesuai.

  2. Tambahkan elemen tambahan apa pun yang Anda inginkan dalam file manifes.

    Untuk mengedit file manifes, gunakan editor teks apa pun yang Anda inginkan. Tetapi, file manifes adalah file skrip yang berisi kode, jadi Anda mungkin ingin mengeditnya di lingkungan pembuatan skrip atau pengembangan, seperti Visual Studio Code. Semua elemen file manifes bersifat opsional, kecuali untuk nomor ModuleVersion .

    Untuk informasi selengkapnya, lihat deskripsi parameter di cmdlet New-ModuleManifest . Untuk deskripsi kunci dan nilai yang dapat Anda sertakan dalam manifes modul, lihat about_Module_Manifests.

  3. Untuk mengatasi skenario apa pun yang mungkin tidak dicakup oleh elemen manifes modul dasar, Anda memiliki opsi untuk menambahkan kode tambahan ke manifes modul Anda.

    Untuk masalah keamanan, PowerShell hanya menjalankan subset kecil operasi yang tersedia dalam file manifes modul. Umumnya, Anda dapat menggunakan if operator pernyataan, aritmetika dan perbandingan, dan jenis data PowerShell dasar.

  4. Setelah membuat manifes modul, Anda dapat mengujinya untuk mengonfirmasi bahwa jalur apa pun yang dijelaskan dalam manifes sudah benar. Untuk menguji manifes modul Anda, gunakan Test-ModuleManifest.

    Test-ModuleManifest myModuleName.psd1

  5. Pastikan manifes modul Anda terletak di tingkat atas direktori yang berisi modul Anda.

    Saat Anda menyalin modul ke sistem dan mengimpornya, PowerShell menggunakan manifes modul untuk mengimpor modul Anda.

  6. Secara opsional, Anda dapat langsung menguji manifes modul dengan panggilan ke Import-Module dengan melakukan sumber titik manifes itu sendiri.

    Import-Module .\myModuleName.psd1

Contoh manifes modul

Contoh manifes modul berikut dibuat dengan New-ModuleManifest di PowerShell 7 dan berisi kunci dan nilai default. Untuk deskripsi terperinci tentang setiap elemen dalam manifes modul, lihat about_Module_Manifests.

#
# Module manifest for module 'SampleModuleManifest'
#
# Generated by: User01
#
# Generated on: 10/15/2019
#

@{

# Script module or binary module file associated with this manifest.
# RootModule = ''

# Version number of this module.
ModuleVersion = '0.0.1'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = 'b632e90c-df3d-4340-9f6c-3b832646bf87'

# Author of this module
Author = 'User01'

# Company or vendor of this module
CompanyName = 'Unknown'

# Copyright statement for this module
Copyright = '(c) User01. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

    PSData = @{

        # Tags applied to this module. These help with module discovery in online galleries.
        # Tags = @()

        # A URL to the license for this module.
        # LicenseUri = ''

        # A URL to the main website for this project.
        # ProjectUri = ''

        # A URL to an icon representing this module.
        # IconUri = ''

        # ReleaseNotes of this module
        # ReleaseNotes = ''

        # Prerelease string of this module
        # Prerelease = ''

        # Flag to indicate whether the module requires explicit user acceptance for install/update/save
        # RequireLicenseAcceptance = $false

        # External dependent modules of this module
        # ExternalModuleDependencies = @()

    } # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

Lihat juga