0

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?

8
  • "But each time, I have issues." what issues? Please post relevant error messages and descriptions of the unexpected behavior you're seeing Commented Feb 21, 2018 at 14:37
  • hi @MathiasR.Jessen see the update. Thanks Commented Feb 21, 2018 at 14:39
  • Try: powershell -Command "& {get-content 'c:\tmp\test.txt' | foreach-object {$_ -replace 'MYID', 'MyValue'} | set-content 'c:\tmp\test.txt'}" Commented Feb 21, 2018 at 14:44
  • Hi @JohnLBevan, it gives me this error i.sstatic.net/LJF2p.png What it says is the file is being used by another process therefore it cannot write on it. Yet the file is closed. I created an another file and same thing. Commented Feb 21, 2018 at 14:49
  • Ah; that's your process; Get-Content reads the first line, passes it down the pipeline to be changed, then Set-Content tries to write it and fails because Get-Content still has the file open for reading. Commented Feb 21, 2018 at 15:14

1 Answer 1

2

You don't need to use Invoke-Command or a script block if you just want to execute a command on the local machine under normal conditions. Rather, we can just use the -Command switch to PowerShell:

powershell -command "(get-content c:\tmp\test.txt) | foreach-object { $_ -replace 'MYID', 'MyValue' } | set-content c:\tmp\test.txt"

Note the single quotes around the -replace strings; this avoids problems with the command processor's escaping. This command works on my machine with a multiline file, but if it gives you trouble with the file still being open, you can use this version, which reads the file in full rather than line by line:

powershell -c "(get-content c:\tmp\test.txt -raw) -replace 'MYID', 'MyValue' | set-content c:\tmp\test.txt -nonewline"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.