1

我正在尝试使用 PowerShell 4.0 将多个模块打包到一个清单模块中。基本上,我有 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..."
}    

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

所以我的具体问题是,当我尝试$firstVariable从 Setup.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 回答