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)
}
我仍然得到同樣的結果的空白,不幸的是 – NealR
@NealR您還需要add'改變''到settings'。查看更新回答 – dotnetom
啊,我明白了。謝謝,仍然在處理PowerShell的語法/約定。這工作:) – NealR