2017-01-07 5 views
0

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

私のコードです:

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(第1モジュール):

$firstVariable # This is not getting exported. Why? 

function Start-FirstSetup{ 

    Register-FirstVariables 

    echo "First setup started..." 
}  

第2および第3のセットアップ:最初のと同じ、唯一varablesや機能がfirstVariable、thirdVariable、開始-SecondSetupを、命名されている私から$firstVariableにアクセスしようとしたときなどは、

だから私の具体的な問題は、ありますSetup.psm1、定義されていないというエラーが表示されます。しかし、私はマニフェストモジュールでエクスポート用にマークしました。それで私はここで何を欠場したのですか? Start-FirstSetupが呼び出されると、問題なく処理され、モジュールをデバッグすることさえできますが、それでも$ firstVariableは未定義です。

答えて

0

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

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

    New-Variable -Name firstvariable 
    

    を暗黙的な定義($firstvariable)が十分ではありません。

  • エクスポートモジュール(.psm1ファイル)内の変数:

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

    VariablesToExport = '*' 
    

    をあなたもリストを提供することができます。

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

関連する問題