Injecting multiple lines of text into a file

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Rich Hopkins Rich Hopkins 1 year, 9 months ago.

  • Author
    Posts
  • #22752
    Profile photo of Rich Hopkins
    Rich Hopkins
    Participant

    I've got a config file that I'm trying to inject some new config data into. I've been trying to get it going using this, but I can't get the content of the file to match more than one line at a time (isn't that what -Raw is for?)

    $originalConfig = @"
    mergeTools.beyondcompare2.commandLine=bc2.exe /title1="Merge From:{1}" /title2="Working File:{3}" "{5}" "{7}" /savetarget="{7}"
    mergeTools.araxis.title=Araxis Merge, Araxis Ltd.
    "@

    $newConfig = @"
    mergeTools.beyondcompare2.commandLine=bc2.exe /title1="Merge From:{1}" /title2="Working File:{3}" "{5}" "{7}" /savetarget="{7}"
    mergeTools.beyondcompare3.title=Beyond Compare 3.x, Scooter Software
    mergeTools.beyondcompare3.commandLine="C\:\\Program Files (x86)\\Beyond Compare 4\\BComp.exe" /title1\="{1}" /title2\="{2}" /title3\="{0}" "{5}" "{6}" "{4}" "{7}"
    mergeTools.araxis.title=Araxis Merge, Araxis Ltd.
    "@

    $configFile = Join-Path "${Env:ProgramFiles(x86)}" "Integrity\IntegrityClient10\IntegrityClientSite.rc"
    (Get-Content $configFile -Raw) -replace $originalConfig, $newConfig | Set-Content $configFile -Force

    Any thoughts on what I'm missing?

  • #22756
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Rich,

    Can you try using the .Replace method of the string instead of -replace. It's probably to do with it interpreting it as containing regex. So something like this.

    (Get-Content $configFile -Raw).Replace($originalConfig, $newConfig) | Set-Content $configFile -Force

  • #22769
    Profile photo of Rich Hopkins
    Rich Hopkins
    Participant

    That did it, thanks!

You must be logged in to reply to this topic.