0

I'm attempting to use the Powershell -replace command to update the data source in my config file. However, the -replace regex below will not remove the $oldServer value.

I've place a string directly in to the $_.connectionString variable in the loop and it saved properly, so I know that is not the issue. Seems to just be the regex.

    #environment variables
    $env = "DEV"                    
    $oldServer = "quasq10"          
    $newValue = "$env-AR-SQL.CORP.COM"

    $doc = [xml](Get-Content "D:\AMS\app.config")      
    $doc.configuration.connectionStrings.add|%{        
        $_.connectionString = $_.connectionString -replace $oldServer, $newValue;
    }
    $doc.Save($file.FullName)

EDIT

Per the comment below I added a Write-host $_.connectionString statement as the first line in the loop. Below is the console output

metadata=res:///MonetDb.csdl|res:///MonetDb.ssdl|res://*/MonetDb.msl;provider=System.Data.SqlClient;provider connection string="data source=quasq10\sql08a;initial catalog=MyDB ;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"

2
  • Well that is a pretty basic statement you have there so to figure out why it isn't matching we would probably need a sample of what is actually in $_.connectionString within your foreach. I suggest sticking a Write-Host $_.connectionString in the foreach to get a sample of the values to be matched and perhaps post them here. Commented Mar 16, 2015 at 19:54
  • Was just thinking the same. Added the connection string value to the post. Commented Mar 16, 2015 at 21:33

1 Answer 1

1

I just put this right into ISE, I copied your connection string into a variable and was able to do this replace as a one off.

$connectionString = 'metadata=res:///MonetDb.csdl|res:///MonetDb.ssdl|res://*/MonetDb.msl;provider=System.Data.SqlClient;provider connection string="data source=quasq10\sql08a;initial catalog=MyDB ;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"'
$env = "DEV"
$oldServer = "quasq10"          
$newValue = "$env-AR-SQL.CORP.COM"

$connectionString -replace $oldServer, $newValue

res:///MonetDb.csdl|res:///MonetDb.ssdl|res://*/MonetDb.msl;provider=System.Data.SqlClient;provider connection string="data source=DEV-AR-SQL.CORP.COM\sql08a;initial catalog=MyDB ;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"

I think your foreach loop might not be getting the info you want, because it looks like your replace is fine.

$doc.configuration.connectionStrings.add

I haven't done much with XML, does the XML data type have an ADD member function? You aren't really adding anything, right?

As a test, what do you get from this:

$doc.configuration.connectionStrings | % {        
    $_.connectionString -replace $oldServer, $newValue;
}

Run that against a dummy file and see what happens.

For a sanity check on the replace operator:

$string = "The quick brown fox jumped over the lazy dog"
$oldColor = "brown"
$newColor = "orange"

$string -replace $oldColor, $newColor

To avoid digging through comments, this method worked

$string.Replace($oldColor,$newColor)
Sign up to request clarification or add additional context in comments.

11 Comments

Interesting - so if I copy/paste the variables/regex from the top of your post into ISE I do not get the same results. The $oldServer value does not get replaced. Any idea why that would be? Haven't touched the code at all, just did a copy/paste.
Also if I set a breakpoint inside the foreach you have in your post, I see that the -replace line is hit, however again the regex fails to replace the old value.
What PowerShell version are you using? I'm using v4, though I'm not sure that would make a difference.
Using $PSVersionTable it looks like I'm on PSVersion 4.0 too.
Odd, just for sanity's sake I copied that back into ISE from my post here, and the -Replace still works as expected. Do you get any errors or warnings when doing the replace?
|

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.