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

Update WCF endpoint nodes in web.config using Powershell

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

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

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

您应该像这样更改您的代码:

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