Issue replacing line with | in the string

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 5 months, 2 weeks ago.

  • Author
    Posts
  • #43574
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    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"

    }

  • #43580
    Profile photo of Craig Duff
    Craig Duff
    Participant

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

  • #43583
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    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"

    }

  • #43585
    Profile photo of random commandline
    random commandline
    Participant

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

    Get-Content .\oldweb.config | Where-Object {$_ -notmatch '\|'} | 
    Set-Content .\web.config
    
  • #43596
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    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"

  • #43598
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    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" 
    
  • #43600
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant
    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" 

  • #43617
    Profile photo of random commandline
    random commandline
    Participant

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

    {$_ -notmatch 'com\|dc2prtsf2'}
    
  • #43621
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

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

  • #43647
    Profile photo of random commandline
    random commandline
    Participant

    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
    
  • #43649
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    • This reply was modified 5 months, 2 weeks ago by Profile photo of Curtis Smith Curtis Smith.

You must be logged in to reply to this topic.