Author Posts

June 16, 2016 at 1:26 pm

I am trying to replace a line in a web.config file. the line has a "|" in the the string. When I run the script it does not replace the entire line. It seems to replace only the text after the | . Below is the script I am using

$list = "C:\zworkingfolders\servers.txt"
$computers = get-content $list
foreach ($server in $computers)
{
# This script is used to make changes to the web.configs for the PTVDataService servers in Production
# Backup web.config in PTVDataService folder
copy-Item "\\$server\c$\program files\SupportSite\PTVDataService\web.config" -Destination "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config"
# Update web.config in PTVDataService folder
get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object {$_ -replace ",' '} | Set-content "\\$server\c$\program files\SupportSite\PTVDataService\web.config"

}

June 16, 2016 at 1:38 pm

Replace uses RegEx and | is a special character in RegEx; it means OR. Since you didn't post the replace strings I can't be certain, but that is the most likely candidate.

One way to fix it is to escape it in the search string \|, instead of |.

Another way is to use the escape function of .Net Regex.

[regex]::Escape("blah|blah")

June 16, 2016 at 1:47 pm

Sorry, I thought that I copied the whole script. See below that also shows the replace strings.

$list = "C:\zworkingfolders\servers.txt"
$computers = get-content $list
foreach ($server in $computers)
{
# This script is used to make changes to the web.configs for the PTVDataService servers in Production
# Backup web.config in PTVDataService folder
copy-Item "\\$server\c$\program files\SupportSite\PTVDataService\web.config" -Destination "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config"
# Update web.config in PTVDataService folder
get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object {$_ -replace ",' '} | Set-content "\\$server\c$\program files\SupportSite\PTVDataService\web.config"

}

June 16, 2016 at 1:48 pm

This will keep lines that do not contain the pipe '|' symbol.

Get-Content .\oldweb.config | Where-Object {$_ -notmatch '\|'} | 
Set-Content .\web.config

June 16, 2016 at 2:21 pm

There are multiple lines in the config that use the |. I am trying to replace one specific line that contains the |,

get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object {$_ -replace ",' '} | Set-content "\\$server\c$\program files\SupportSite\PTVDataService\web.config"

June 16, 2016 at 2:23 pm

For some reason this forum is cutting off my code

get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object {$_ -replace '','  '} | Set-content "\\$server\c$\program files\SupportSite\PTVDataService\web.config" 

June 16, 2016 at 2:25 pm

get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object
 {$_ -replace '','  
 key="RGN_0170"value="ORACLE,dc2prtsf2-scan.es.ad.adp.com|dc2prtsf2-scan.es.ad.adp.com,0037,epc22p_svc1,P,EPCLOS2PTV"/>'} | 
Set-content "\\$server\c$\program files\SupportSite\PTVDataService\web.config" 

June 16, 2016 at 3:09 pm

If that is the line you want to remove, use something like this.

{$_ -notmatch 'com\|dc2prtsf2'}

June 16, 2016 at 3:19 pm

I don't want to remove the line. I want to replace it with the new line

June 16, 2016 at 5:17 pm

Ok, I was confused by your code. If you match a part of the line, this should work.

$newline = 'your-new-line...>'
(Get-Content .\oldweb.config) | ForEach-Object {
If ($_ -match 'part-of-line'){$_ -replace $_,$newline}Else{$_}
} | Set-Content .\web.config

June 16, 2016 at 6:08 pm

  • This reply was modified 2 years, 3 months ago by  Curtis Smith.