在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) "configGlobalConfiguration.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) "configOakton_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

在我的主.ps1

Get-ConfigSettings
$LogFilePath #this variable is empty string

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

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

阅读并关注Export-ModuleMember文档:

-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 

最新更新