1

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) 
}

1 Answer 1

2

You have XML elements with periods (Periscope.My.MySettings), so you must escape them with ' so xml correctly treats it as a single element, not 3 separate elements. You also need to change add to setting, because add does not exist in case of settings, it only exists in connection strings.

You should change your code like this:

$doc.configuration.applicationSettings.'Periscope.My.MySettings'.setting | foreach {
    # Your other code
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still getting the same blank results, unfortunately
@NealR You also need to change add to settings. See updated answer
Ah, I see. Thanks, still getting a handle on the powershell syntax/conventions. This worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.