2012-12-04 84 views
11

假設我有一個名爲Foo.ps1如何將配置文件引入Powershell腳本?

PowerShell腳本

我想向大家介紹所謂Foo.ps1.config

一個XML配置文件,在那裏我可以指定我的環境設置是這樣的:

<FunctionsDirectory> 
    $ScriptDirectory\Functions 
</FunctionsDirectory> 
<ModulesDirectory> 
    $ScriptDirectory\Modules 
</ModulesDirectory> 

然後我想在Foo.ps1的開頭加載這個配置,這樣我就可以將我的模塊導入到函數目錄。

我如何在Powershell中實現這一點?

+1

我發現了這個網站,它看上去很聰明給我的: http://www.bjd145.org/2008/01/powershell-and-xml-configuration-files.html – pencilCake

+0

或這一個: http://rkeithhill.wordpress.com/2006/06/01/creating-and-using-a-configuration-file-for-your-powershell-scripts/ – pencilCake

+0

我以前使用過Keith的解決方案,工作一種享受。 – nimizen

回答

7

基於Keith's solution ...代碼加載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]{ 
     } 
    } 

從XML值填充變量:

  $variable1 = $appSettings["var1"] 
      $variable2 = $appSettings["var2"] 

和相關的XML:

<?xml version="1.0"?> 
<configuration> 
    <startup> 
    </startup> 
    <appSettings> 
<!--Vars --> 
    <add key="var1" value="variableValue1"/> 
    <add key="var2" value="variableValue2"/> 
    </appSettings> 
</configuration> 
9

可能一個更簡單的解決方案.... 假設您的配置文件名稱爲「Con fix.xml」,試試這個:

PS Testing> [xml]$configFile= get-content .\Config=.xml 
PS Testing> $configFile 
xml         configuration 
---         ------------- 
version="1.0"      configuration 

讀出數據新變量:

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 

長話短說:你要把你的變量,XML,和做一個獲取內容。

在這種情況下,Config.xml中看起來是這樣的:

<?xml version="1.0"?> 
<configuration> 
    <startup> 
    </startup> 
    <appSettings> 
    <!--Vars --> 
    <add key="var1" value="variableValue1"/> 
    <add key="var2" value="variableValue2"/> 
    </appSettings> 
</configuration> 
6

對於替代XML配置,如果你是靈活使用其他類型的配置。我建議使用全局PS配置文件,具體方法如下:

創建Powershell配置文件(例如Config.ps1),然後將所有配置設置爲全局變量並將其作爲第一步初始化,以便配置值在您的腳本上下文。

這種方法的好處是可以在Config.ps1 PS文件中使用各種類型的數據結構,如標量變量,集合和散列,並在PS代碼中很容易引用。

下面是在操作的示例:

enter image description here

這裏是C:\配置\ Config.ps1文件:

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

    varCollection = @{  
     item0  = "colValue0" 
     item1 = "colValue1" 
     item2 = "colValue2" 
    }  
} 

然後加載功能從配置/變量。在該模塊C PS1文件:\模塊\ PSModule.psm1,像這樣:

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

foreach ($script in $scriptFiles) 
{ 
    try 
    {  
     . $script.FullName 
    } 
    catch [System.Exception] 
    { 
     throw 
    } 
} 

拉狡猾,初始化腳本包含下面這一行:(C:\ Init.ps1)。

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

運行完Init.ps1後, global:config變量將在您的腳本上下文中可用。下面是輸出:
enter image description here

相關問題