如何使用PowerShell中的模块清单从多个文件导出变量?

时间:2017-01-07 18:35:18

标签: powershell-v4.0 powershell-module

我正在尝试使用PowerShell 4.0在一个清单模块中打包多个模块。基本上,我有3个安装模块,可以做一些事情。我使用我的清单模块打包这三个。然后我只导出所有三个模块中的一些函数和变量。但是,只有我的函数可以从外部调用,我的变量无处可见。有人可以帮我从这里出去吗?我基本上遵循了guide

这是我的代码:

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..."
}    

第二和第三次设置:与第一次设置相同,只有变量和函数被命名为firstVariable,thirdVariable,Start-SecondSetup等。

所以我的具体问题是,当我尝试从Setup.psm1访问$firstVariable时,我收到一个未定义的错误。但我在清单模块中将其标记为导出。那我在这里想念的是什么?调用Start-FirstSetup时,它会毫无问题地运行,我甚至可以调试我的模块,但即使这样,我的$ firstVariable也是未定义的。

1 个答案:

答案 0 :(得分:1)

您需要做三件事才能从模块中导出变量:

  • 明确定义变量:

    New-Variable -Name firstvariable
    

    隐式定义($firstvariable)是不够的。

  • 导出模块中的变量(.psm1文件):

    Export-ModuleMember -Variable firstvariable
    
  • 在清单中定义导出的变量(.psd1文件)。通配符通常应该足够了:

    VariablesToExport = '*'
    

    但你也可以提供一个清单:

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

    此设置基本上是一个过滤器,用于定义实际显示哪些导出变量。如果变量在这里没有匹配,则不会从模块中导出,即使它是在.psm1文件中导出的。