在Powershell模块中设置多个变量并将其返回到主程序

时间:2017-11-08 06:11:39

标签: powershell

我编写了一个Powershell模块,它有一些函数,我在如下定义的函数中设置了多个变量:

#
# ConfigurationHelper.psm1
#
# Global Variables
$PackageLocation = ""
$LogFilePath = ""
$LogFileName = ""
$DestinationLocation = ""
$ExcludedBinariesFiles = ""
$ExcludedBinariesFolders = ""
$IncludeTransformsFiles = ""
# end global variables
# Function to read all the config settings 
function Get-ConfigSettings {
    Write-Host "Get-ConfigSetting function is called"
    #logging configuration
    [xml] $logConfigFile = Get-Content -Path (Join-Path ((Get-Item $PSScriptRoot).Parent.FullName) "\config\GlobalConfiguration.xml")
    $LogFilePath = $logConfigFile.SelectSingleNode("/configuration/LogsPath").InnerText;
    $LogFileName = $logConfigFile.SelectSingleNode("/configuration/LogsFileName").InnerText;
    $PackageLocation = (Get-Item $PSScriptRoot).Parent.FullName

    # BinariesConfiguration
    [xml] $BinariesConfig = Get-Content -Path (Join-Path ((Get-Item $PSScriptRoot).Parent.FullName) "\config\Oakton_Environments.xml")
    $environemntNodes = $BinariesConfig.SelectNodes("//environment[@Server=$env:computername]")
    if ($environemntNodes -ne 1) {
        throw "Server configuration missing or more than one environment configuration was found for server"
    }
}
Export-ModuleMember -Function * -Variable $LogFilePath

在我的main.ps1

Get-ConfigSettings

$LogFilePath #this variable is empty string

即使在执行设置变量的函数之后,该变量也是空字符串。我在模块脚本的末尾做了导出成员。如何返回模块中定义的变量?

我想返回configurationHelper.psm1顶部设置的多个变量。

1 个答案:

答案 0 :(得分:0)

阅读并关注Export-ModuleMember docs

-Variable <String[]>
    Specifies the variables that are exported from the script module file. 
    Enter the **variable names**, **without a dollar sign**.
    Wildcard characters are permitted.

使用

Export-ModuleMember -Function * -Variable LogFilePath