Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell - Replacing text and inserting a new line in a file with "\" character

$
0
0

Greetings all,

I'm trying to automate updating a configuration file at remote sites.  I need to replace a string in a line, then insert a new line below it with another string.

So from this:

     Sometext

     SessionConfig=All \

     Sometext

To this:

     Sometext

     SessionConfig=ICA \

     Sessionreliability=YES \

     Sometext

I've gone at this many different ways, each one failing.  depending on the script I get something like this:

     SessionConfig=All SessonConfig=YES \\

Or some other bad variation.  Below is one attempt:

$replacement="SessionConfig=ICA \ `nSessionReliability=YES \"

(get-content wnos.ini) | foreach-object {$_ -replace "SessionConfig=All \",$replacement}|set-content wnos.ini

And another that just tries to add the new line:

(get-content $filename) |

foreach-object

{

$_

if ($_ -match "SessionConfig=ICA \")

     {

     "SessionReliability=YES \"

     }

} |set-content $file

Any help would be appreciated, thanks.




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>