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

How to export variables from multiple files using module manifests in PowerShell?

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

这是我的代码:

设置。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 也是未定义的。

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

  • 显式定义变量:

    New-Variable -Name firstvariable
    

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

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

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

    VariablesToExport = '*'
    

    但您也可以提供一个列表:

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

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