PowerShell モジュール マニフェストを記述する方法

PowerShell モジュールを記述したら、モジュールに関する情報を含むオプションのモジュール マニフェストを追加できます。 たとえば、作成者を記述したり、モジュール内のファイル (入れ子になったモジュールなど) を指定したり、スクリプトを実行してユーザーの環境をカスタマイズしたり、ファイルの種類と書式設定を読み込み、システム要件を定義したり、モジュールがエクスポートするメンバーを制限したりできます。

モジュール マニフェストの作成

モジュール マニフェスト は、モジュールの内容を記述し、モジュールの処理方法を決定する PowerShell データ ファイル (.psd1) です。 マニフェスト ファイルは、キーと値のハッシュ テーブルを含むテキスト ファイルです。 マニフェスト ファイルをモジュールにリンクするには、マニフェストにモジュールと同じ名前を付け、モジュールのルート ディレクトリにマニフェストを格納します。

1 つの .psm1 またはバイナリ アセンブリのみを含む単純なモジュールの場合、モジュール マニフェストは省略可能です。 ただし、可能な限りモジュール マニフェストを使用することをお勧めします。モジュール マニフェストは、コードを整理し、バージョン管理情報を維持するのに役立ちます。 また、グローバル アセンブリ キャッシュにインストールされているアセンブリをエクスポートするには、モジュール マニフェストが必要です。 更新可能なヘルプ機能をサポートするモジュールには、モジュール マニフェストも必要です。 更新可能なヘルプでは、モジュール マニフェストの HelpInfoUri キーを使用して、モジュールの更新されたヘルプ ファイルの場所を含むヘルプ情報 (HelpInfo XML) ファイルを検索します。 更新可能なヘルプの詳細については、「更新可能なヘルプ のサポートを参照してください。

モジュール マニフェストを作成して使用するには

  1. モジュール マニフェストを作成するベスト プラクティスは、New-ModuleManifest コマンドレットを使用することです。 パラメーターを使用して、マニフェストの既定のキーと値を 1 つ以上指定できます。 唯一の要件は、ファイルに名前を付ける必要があります。 New-ModuleManifest 指定した値を使用してモジュール マニフェストを作成し、残りのキーとその既定値を含めます。 複数のモジュールを作成する必要がある場合は、New-ModuleManifest を使用して、異なるモジュールに対して変更できるモジュール マニフェスト テンプレートを作成します。 既定のモジュール マニフェストの例については、サンプル モジュール マニフェストのを参照してください。

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

    別の方法として、必要最小限の情報である ModuleVersionを使用して、モジュール マニフェストのハッシュ テーブルを手動で作成することもできます。 モジュールと同じ名前でファイルを保存し、.psd1 ファイル拡張子を使用します。 その後、ファイルを編集し、適切なキーと値を追加できます。

  2. マニフェスト ファイルに必要な要素を追加します。

    マニフェスト ファイルを編集するには、任意のテキスト エディターを使用します。 ただし、マニフェスト ファイルはコードを含むスクリプト ファイルであるため、Visual Studio Code などのスクリプトまたは開発環境で編集することもできます。 ModuleVersion 番号を除き、マニフェスト ファイルのすべての要素は省略可能です。

    詳細については、New-ModuleManifest コマンドレットのパラメーターの説明を参照してください。 モジュール マニフェストに含めることができるキーと値の説明については、 about_Module_Manifestsを参照してください。

  3. 基本モジュール マニフェスト要素でカバーされない可能性があるシナリオに対処するには、モジュール マニフェストにコードを追加するオプションがあります。

    セキュリティ上の懸念がある場合、PowerShell はモジュール マニフェスト ファイルで使用可能な操作のごく一部のみを実行します。 一般に、if ステートメント、算術演算子と比較演算子、および基本的な PowerShell データ型を使用できます。

  4. モジュール マニフェストを作成したら、それをテストして、マニフェストに記述されているパスが正しいことを確認できます。 モジュール マニフェストをテストするには、Test-ModuleManifest 使用します。

    Test-ModuleManifest myModuleName.psd1

  5. モジュール マニフェストが、モジュールを含むディレクトリの最上位に配置されていることを確認します。

    モジュールをシステムにコピーしてインポートすると、PowerShell はモジュール マニフェストを使用してモジュールをインポートします。

  6. 必要に応じて、マニフェスト自体をドット ソーシングすることで、Import-Module 呼び出してモジュール マニフェストを直接テストできます。

    Import-Module .\myModuleName.psd1

サンプル モジュール マニフェスト

次のサンプル モジュール マニフェストは、PowerShell 7 で New-ModuleManifest で作成され、既定のキーと値が含まれています。 モジュール マニフェスト内の各要素の詳細については、 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 = ''

}

こちらもご覧ください