I need to update a set of WCF endpoints in our web.config and I'd like to add this as a command in a powershell script. Below is the script as I currently have it written. It will update the ConnectionString nodes just fine, however it cannot find any of the value nodes in the Periscope.My.MySettings section.
The loop will execute a specific number of times, however the value in the $_.value variable is empty every time.
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)
}