2015-04-07 47 views
1

我需要在我們的web.config中更新一組WCF端點,並且希望將其添加爲PowerShell腳本中的命令。以下是我現在編寫的腳本。它將更新ConnectionString節點,但它無法找到Periscope.My.MySettings部分中的任何value節點。使用Powershell更新web.config中的WCF端點節點

循環將執行特定次數,但每次變量$_.value中的值都爲空。

的web.config

<applicationSettings> 
    <Periscope.My.MySettings> 
    <setting name="Periscope_periscopeSearch_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeSearch/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveRelationships_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveRelationships/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveProducers_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveProducers/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveMasterSubs_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveMasterSubs/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveGeneral_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveGeneral/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveComments_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveComments/Service1.asmx</value> 
    </setting> 
    <setting name="Periscope_periscopeRetrieveProfiles_Service1" serializeAs="String"> 
     <value>http://inside.com/periscopeRetrieveProfiles/Service1.asmx</value> 
    </setting> 
    </Periscope.My.MySettings> 
</applicationSettings> 

Powershell的

$dir = Get-ChildItem $directory -Recurse 
$config = $dir | where {$_.extension -eq ".config"} 

#generated config connection strings 
foreach($file in $config) 
{ 
    Write-Host "Updating $file" -ForegroundColor Green  
    $doc = [xml](Get-Content $file.FullName) 

    #THIS WORKS FINE 
    if($doc.configuration.connectionStrings){ 
     $doc.configuration.connectionStrings.add |%{ 
      if($_.connectionString.ToLower() -match $oldDb.ToLower()){ 
       $_.connectionString = $_.connectionString.ToLower().replace($oldDb.ToLower(), $newDb) 
      } 
     } 
    } 

    #THE $_.value VARIABLE IS EMPTY EVERY TIME 
    $doc.configuration.applicationSettings.Periscope.My.MySettings.add | foreach { 
     if($_.value){ 
      $tempString = $_.value.ToLower() 
      if($tempString -match $oldDb.ToLower()){ 
       $_.value = $tempString.replace($oldDb, $newDb) 
      } 
      elseif($tempString -match $oldDomain.ToLower()){ 
       $_.value = $tempString.replace($oldDomain.ToLower(), $newDomain) 
      } 
     } 
    } 

    $doc.Save($file.FullName) 
} 

回答

2

您有周期(Periscope.My.MySettings)XML元素,所以你必須用'逃避他們,讓XML正確對待它作爲單個元素,而不是3個單獨的元素。您還需要將add更改爲setting,因爲add在設置的情況下不存在,它只存在於連接字符串中。

你應該改變你的代碼是這樣的:

$doc.configuration.applicationSettings.'Periscope.My.MySettings'.setting | foreach { 
    # Your other code 
} 
+0

我仍然得到同樣的結果的空白,不幸的是 – NealR

+0

@NealR您還需要add'改變''到settings'。查看更新回答 – dotnetom

+0

啊,我明白了。謝謝,仍然在處理PowerShell的語法/約定。這工作:) – NealR