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”
}
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.
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”
}
This will keep lines that do not contain the pipe ‘|’ symbol.
Get-Content .\oldweb.config | Where-Object {$_ -notmatch '\|'} |
Set-Content .\web.config
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”
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”
get-content "\\$server\c$\program files\SupportSite\PTVDataService\web$(get-date -f yyyy-MM-dd).config" | ForEach-Object
{$_ -replace '',' <add
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"
If that is the line you want to remove, use something like this.
{$_ -notmatch 'com\|dc2prtsf2'}
I don’t want to remove the line. I want to replace it with the new line
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