如何将配置文件引入Powershell脚本?

pen*_*ake 21 .net xml powershell configuration app-config

假设我有一个名为Foo.ps1的Powershell脚本

我想介绍一个名为Foo.ps1.config的XML配置文件

我可以在哪里指定我的环境设置:

<FunctionsDirectory>
     $ScriptDirectory\Functions
</FunctionsDirectory>
<ModulesDirectory>
     $ScriptDirectory\Modules
</ModulesDirectory>
Run Code Online (Sandbox Code Playgroud)

然后我想在Foo.ps1的开头加载这个配置,这样我就可以导入我的模块并点到函数目录.

我怎样才能在Powershell中实现这一目标?

小智 19

可能是一个更简单的解决方案....假设您的配置文件名称为"Confix.xml",请尝试以下操作:

PS Testing> [xml]$configFile= get-content .\Config=.xml
PS Testing> $configFile
xml                                  configuration
---                                  -------------
version="1.0"                        configuration
Run Code Online (Sandbox Code Playgroud)

从新变量中读取数据:

PS Testing> $configFile.configuration.appsettings
#comment                             add
--------                             ---
Vars                                 {add, add}

PS Testing> $configFile.configuration.appsettings.add
key                                  value
---                                  -----
var1                                 variableValue1
var2                                 variableValue2

PS Testing> $configFile.configuration.appsettings.add[0].value
variableValue2
Run Code Online (Sandbox Code Playgroud)

简而言之:将变量转换为XML,并执行get-content.

在这种情况下,Config.xml如下所示:

<?xml version="1.0"?>
<configuration>
  <startup>
  </startup>
  <appSettings>
    <!--Vars -->
    <add key="var1" value="variableValue1"/>
    <add key="var2" value="variableValue2"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)


yan*_*taq 14

如果您可以灵活地使用其他类型的配置,则可以替代XML配置.我建议使用全局PS配置文件,其方法如下:

创建一个Powershell配置文件(例如Config.ps1),然后将所有配置作为全局变量并作为第一步启动它,以便配置值在脚本上下文中可用.

这种方法的好处是可以在Config.ps1 PS文件中使用各种类型的数据结构,如标量变量,集合和散列,并在PS代码中轻松引用.

以下是一个实例:

在此输入图像描述

这是C:\ Config\Config.ps1文件:

$global:config = @{   
    Var1 = "Value1"

    varCollection = @{       
        item0     = "colValue0"
        item1   = "colValue1"
        item2  = "colValue2"
    }       
}
Run Code Online (Sandbox Code Playgroud)

然后从此模块C:\ Module\PSModule.psm1中的Config.ps1文件中加载函数/变量,如下所示:

$scriptFiles = Get-ChildItem "$PSScriptRoot\Config\*.ps1" -Recurse

foreach ($script in $scriptFiles)
{
    try
    {       
        . $script.FullName 
    }
    catch [System.Exception]
    {
        throw
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,初始化脚本包含以下一行:(C:\ Init.ps1).

Import-Module $PSScriptRoot\Module\PSModule.psm1 -Force

运行Init.ps1之后.global:config变量将在您的脚本上下文中可用.这是输出:
在此输入图像描述


nim*_*zen 11

基于Keith的解决方案 ...加载XML的代码:

   $configFile = "c:\Path2Config"
    if(Test-Path $configFile) {
        Try {
            #Load config appsettings
            $global:appSettings = @{}
            $config = [xml](get-content $configFile)
            foreach ($addNode in $config.configuration.appsettings.add) {
                if ($addNode.Value.Contains(‘,’)) {
                    # Array case
                    $value = $addNode.Value.Split(‘,’)
                        for ($i = 0; $i -lt $value.length; $i++) { 
                            $value[$i] = $value[$i].Trim() 
                        }
                }
                else {
                    # Scalar case
                    $value = $addNode.Value
                }
            $global:appSettings[$addNode.Key] = $value
            }
        }
        Catch [system.exception]{
        }
    }
Run Code Online (Sandbox Code Playgroud)

要从XML值填充变量:

            $variable1 = $appSettings["var1"]
            $variable2 = $appSettings["var2"]
Run Code Online (Sandbox Code Playgroud)

以及相关的XML:

<?xml version="1.0"?>
<configuration>
  <startup>
  </startup>
  <appSettings>
<!--Vars -->
    <add key="var1" value="variableValue1"/>
    <add key="var2" value="variableValue2"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)


cjb*_*110 6

另一个解决方案(类似于@yantaq)就是有一个单独的脚本并加载它。

所以config.ps1包含:

$Environment = $Environment.ToUpper()
$GDriveUNC = "\\servername.domain.net\Data"
$BinLocation = "$pwd\bin-$Environment"
Run Code Online (Sandbox Code Playgroud)

请注意,在本例中$Environment是主脚本的一个参数,您可以进行任何您想要的操作。

然后在主脚本中,只需运行此“配置”脚本,如下所示:

. $PSScriptRoot\config.ps1
Run Code Online (Sandbox Code Playgroud)