使用Powershell更新web.config中的WCF端点节点

时间:2015-04-07 17:16:30

标签: c# wcf powershell config

我需要在web.config中更新一组WCF端点,并且我想在PowerShell脚本中将其添加为命令。下面是我目前编写的脚本。它会很好地更新ConnectionString个节点,但它找不到value部分中的任何Periscope.My.MySettings个节点。

循环将执行特定次数,但$_.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) 
}

1 个答案:

答案 0 :(得分:2)

您有包含句点(Periscope.My.MySettings)的XML元素,因此必须使用'对它们进行转义,以便xml正确地将其视为单个元素,而不是3个单独的元素。您还需要将add更改为setting,因为在设置情况下add不存在,它只存在于连接字符串中。

您应该像这样更改代码:

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