When running the part below through PowerShell prompt, it does what it is supposed to do - change anything that contains MYID to MyValue.
(Get-Content C:/tmp/test.txt) | ForEach-Object {$_ -replace "MYID", "MyValue"} | Set-Content C:/tmp/test.txt
Yet when I'm running it through a script block like below, it fails:
PowerShell Invoke-Command -ScriptBlock {Get-Content C:/tmp/test.txt | ForEach-Object {$_ -replace "MYID", "MyValue"} | Set-Content C:/tmp/test.txt}
Below is the trace of the command above
λ powershell invoke-command -scr {get-content c:\tmp\test.txt | foreach-object {$_ -replace "MYID", "MyValue"} | set-content c:\tmp\test.txt} 'foreach-object' n’est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes.
I tried to do diverses variations like the one below
powershell invoke-command -scr {(get-content c:\tmp\test.txt) | (foreach-object {$_ -replace "MYID", "MyValue"}) | (set-content c:\tmp\test.txt)}
The command above, gives me the following error
} was not expected.
Any ideas?
powershell -Command "& {get-content 'c:\tmp\test.txt' | foreach-object {$_ -replace 'MYID', 'MyValue'} | set-content 'c:\tmp\test.txt'}"