1

PowerShell 4.0 を使用して、複数のモジュールを 1 つのマニフェスト モジュールにパッケージ化しようとしています。基本的に、いくつかのことを行う 3 つのセットアップ モジュールがあります。これら 3 つをマニフェスト モジュールを使用してパッケージ化します。次に、3 つのモジュールすべてから一部の関数と変数のみをエクスポートします。ただし、外部から呼び出すことができるのは関数だけで、変数はどこにも表示されません。誰か助けてくれませんか?私は基本的にこのガイドに従いました。

これが私のコードです:

Setup.ps1 (スタートアップ スクリプト):

$currentDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition 
$setupScriptPath = $currentDirectory + "\Setup.psd1"

Import-module $setupScriptPath

# This has to be set for every environment    
$firstVariable # Not defined?

# $secondVariable= "http://url/"
# $thirdVariable= "http://url/"

Start-FirstSetup

Remove-Module -Name Setup

Setup.psd1 (マニフェスト モジュール):

#
# Module manifest for module 'Setup'
#
# Generated by: Name
#
# Generated on: 1/7/2017
#

@{

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

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

# ID used to uniquely identify this module
GUID = 'guid'

# Author of this module
Author = 'Author name'

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

# Copyright statement for this module
Copyright = '(c) 2017 Company'

# Description of the functionality provided by this module
Description = 'Starts three setups.'

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

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

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

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# 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 = @('FirstSetup.psm1', 'SecondSetup.psm1', 'ThirdSetup.psm1')

# Functions to export from this module
FunctionsToExport = @('Start-FirstSetup', 'Start-SecondSetup', 'Start-ThirdSetup')

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = @('firstVariable', 'secondVariable', 'thirdVariable')

# Aliases to export from this module
AliasesToExport = '*'

# List of all modules packaged with this module
ModuleList = @('FirstSetup.psm1', 'SecondSetup.psm1', 'ThirdSetup.psm1')

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

# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ''

# HelpInfo URI of this module
# HelpInfoURI = ''

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

}

FirstSetup.psm1 (最初のモジュール):

$firstVariable # This is not getting exported. Why?

function Start-FirstSetup{

    Register-FirstVariables

    echo "First setup started..."
}    

2 番目と 3 番目のセットアップ: 最初と同じで、変数と関数のみが firstVariable、 thirdVariable、Start-SecondSetup などの名前になります。

私の特定の問題は、$firstVariableSetup.psm1 からアクセスしようとすると、定義されていないというエラーが表示されることです。しかし、マニフェスト モジュールでエクスポート用にマークしました。それで、私はここで何を逃したのですか?がStart-FirstSetup呼び出されると問題なく実行され、モジュールをデバッグすることもできますが、それでも $firstVariable は定義されていません。

4

1 に答える 1

1

モジュールからエクスポートされた変数を取得するには、次の 3 つのことを行う必要があります。

  • 変数を明示的に定義します。

    New-Variable -Name firstvariable
    

    暗黙の定義 ( $firstvariable) では十分ではありません。

  • モジュール (.psm1 ファイル) 内の変数をエクスポートします。

    Export-ModuleMember -Variable firstvariable
    
  • エクスポートされた変数をマニフェスト (.psd1 ファイル) で定義します。通常、ここではワイルドカードで十分です。

    VariablesToExport = '*'
    

    リストを提供することもできます。

    VariablesToExport = @('firstvariable', 'secondvariable', 'thirdvariable')
    

    この設定は基本的に、エクスポートされた変数のどれが実際に公開されるかを定義するためのフィルターです。変数がここで一致しない場合、たとえ .psm1 ファイルでエクスポートされたとしても、モジュールからエクスポートされません。

于 2017-01-08T00:41:38.890 に答える