Powershell String Replacement in File -


i'm trying replace few strings within powershell file.

$source_ip = read-host 'enter source ip' $target_ip = read-host 'enter target ip' 

by using following line in powershell script, file shows modified, changes don't take effect.

(get-content "c:\solutions.ps1") -replace "$target_ip = read-host 'enter target ip'", "$target_ip = '192.168.0.221'" | set-content "c:\solutions.ps1" 

any reason why changes don't take effect?

this running administrator, on windows server 2008, powershell version 2 believe.

as petseral points out -replace comparison operator supports regex. while can have degree of expressions in patterns adding unnecessary amount of complexity since using simple matches anyway.

the easier solution use string method .replace(). since not array operator , want replace every occurrence reading in file single string make simple action.

$filepath = "c:\solutions.ps1" (get-content $filepath | out-string).replace($source_ip,$target_ip) | set-content $filepath 

note replace case sensitive. if replacing ip addresses moot point. unsure why having issues second file.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -