PowerShell Find and Replace in file issues - powershell

I am having trouble finding how to find and replace in a file using poweshell. The file has several lines that are similar. I need to change the "ACT" to "RDY" in the line that starts with <PC_4 . All Lines start with < and end with >.
The Code:
powershell -Command(Get-Content C:\testfodler\test.XML) -replace '<PC_4 SET_THING="ACT">', '<PC_4 SET_THING="RDY">' | Out-File -encoding ASCII myFile.txt
The error I get is:
The '<' operator is reserved for future use.
Not all parse errors were reported. Correct the reported errors and try again.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
Running this on a windows 10 machine.

First, you should quote the value (code) passed to the -Command parameter because it expects a string. How you quote depends on how you call powershell.exe.
If you are in PowerShell or CMD and are calling powershell.exe, you may surround the code passed to -Command with double quotes and double quote escape ("") inner double quotes, which is the PowerShell layer of escaping. Then backslash escape the escaping quote, which is for the cmd layer of escaping.
powershell.exe -command "(Get-Content C:\testfolder\test.XML) -replace '<PC_4 SET_THING=\""ACT\"">', '<PC_4 SET_THING=\""RDY\"">' | Set-Content myfile.txt -encoding ASCII"

Related

Calling Powershell script from CMD with paths as parameters

I`m having trouble make this cmd command to run powershell script by passing two parameters (paths).
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Test\testing.ps1" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\"
I`m getting the error below:
At line:1 char:36
+ ... ps\Scripts\Test\testing.ps1 "\\Data1\dataholder$\office\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\\Data1\dataholder$\office' in expression or statement.
At line:1 char:146
+ ... Smith\backup" "\\Data1\dataholder$\office\J Smith\backup2"
+ ~
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Any idea of why this is failing?
I had to modify the path leading to script. Now the command look like that:
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Power test\testing.ps1" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\"
This is failing due to the space in the script path, any idea how to handle this?
You need to backslash escape quotes that you want to be sent from cmd shell to PowerShell:
powershell -command "& \"C:\Apps\Scripts\Power test\testing.ps1\" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\""
If your program or script file has spaces, you need to call it with a call operator (&) and include that in your string that is sent to -Command. Notice the double quotes around the entire value/string that maps to -Command.
Note: Inner double quotes work here because $ is succeeded by \. If you had dataholder$folder as an example, you would need single quotes or to escape the $ because PowerShell would try to interpret $folder as a variable.
Here's a backquoting the spaces approach.
type "my script.ps1"
echo $args[0]
powershell .\my` script.ps1 hi` there
hi there
Or strategic placement of single quotes. Something can be executed if the first character isn't a quote. The quote didn't work before the backslash.
powershell .\'my script.ps1' 'hi there'
hi there

Passing Powershell from Cmd with variable evaluation

I want to use the following command
powershell -command "get-content %CONFIG_FILE% | %{$_ -replace \"APP_PATH=.+\",\"APP_PATH=%APP_DIR%\"}"
but with the evaluation of the %CONFIG_FILE% and the %APP_DIR% which are defined in the batch script using
set CONFIG_FILE=C:\B0_GW.cfg
set APP_DIR=C:\dbg\kernel
when i do so, i currently get the following issue:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
any ideas?
aschipfl has provided the crucial pointer in a comment on the question:
From within a batch file[1], you must escape % characters that you want to pass through to the target application as %%.
Otherwise, cmd interprets % as the start or end of an environment-variable reference (which in part happens by design in your command; e.g., %CONFIG_FILE%).
(You've already correctly escaped embedded " chars. in your command as \" for PowerShell).
Specifically, the % in the %{...} part of your command needs escaping (% is PowerShell's built-in alias for the ForEach-Object cmdlet):
powershell -command "get-content %CONFIG_FILE% | %% {$_ -replace \"APP_PATH=.+\",\"APP_PATH=%APP_DIR%\"}"
Alternatively, simply use the cmdlet's actual name, ForEach-Object, which obviates the need for escaping:
powershell -command "get-content %CONFIG_FILE% | ForEach-Object {$_ -replace \"APP_PATH=.+\",\"APP_PATH=%APP_DIR%\"}"
[1] Sadly, the behavior at cmd.exe's command prompt (in an interactive session) differs - see this answer.

Environment variable, PowerShell session vs. started from CMD

I am trying to run a PowerShell command from within a command prompt window (run as Administrator), but it fails. Whereas when I run the same command from within a PowerShell window it runs fine.
Here is the command without error in a PowerShell window:
Powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
In the command prompt window it fails:
C:\test>powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+ ~
Missing ')' in method call.
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+ ~~~~~~~~
Unexpected token 'HostIPv4' in expression or statement.
At line:1 char:47
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+ ~
Missing argument in parameter list.
At line:1 char:73
+ ... ironment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Machine)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
What could be the problem?
PowerShell's command-line parsing removes the double quotes, try using single quotes:
powershell [Environment]::SetEnvironmentVariable('HostIPv4', '192.168.255.14:', 'Machine')
Also note that you have to reopen an new process window to see the results (this is a known behavior for the command shell, see also: C# set environment variable)
iRon's helpful answer explains the problem and works, but I suggest adopting a generally more robust approach to invoking PowerShell commands from cmd.exe:
Use -Command explicitly, because in PSv6 the default will change to -File, expecting a script filename rather than a command.
Use -NoProfile, to avoid unnecessary loading of the PowerShell profiles and for a more predictable execution environment.
Double-quote your entire PowerShell command to protect it from potentially unwanted up-front interpretation by cmd.exe.
powershell -NoProfile -Command "[Environment]::SetEnvironmentVariable('HostIPv4', '192.168.255.14:', 'Machine')"
The use of ' instead of " inside the command string is an easy way to avoid having to escape embedded " characters, which works fine here, but in case you do need embedded " (for interpolating strings), escape them either as \" (sic) or """ (sic).

Powershell command -replace with multi-line value

I'm struggling to use powershell to replace a string with multi-line value.
The value is from Jenkins input text parameter. So this value is a multi-line string.
I use powershell to replace {{BUILD_INFO_CHANGES}} with %BUILD_INFO_CHANGES%.
The %BUILD_INFO_CHANGES% value is
-bug1
-bug 2
Here is the script:
powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', '%BUILD_INFO_CHANGES%' | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"
However, I got the error response from Jenkins.
'{{BUILD_INFO_FIXED_BUGS}}', <<<< '- bug1 is missing the terminator: '. ... + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
d
And I change the script and use # to wrap the value. Here is the changed script.
powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', #'%BUILD_INFO_CHANGES%'# | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"
I got another error.
FullyQualifiedErrorId : UnrecognizedToken
Does anyone have a solution for this?
thanks!
The problem is using the -Command parameter for anything other than a simple script you will run into issues where characters such as curly braces and quotes will be misinterpreted by the command prompt before the are they are passed to PowerShell. You could could tie yourself in knots by adding several layers of escaping or there is a simpler way - use the -EncodedCommand parameter instead.
For the -EncodedCommand you just need to Base64 encode your command which you can do with the following PowerShell script:
$command = #'
# Enter your commands containg curly braces and quotes here
# As long as you like
'#
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes) | clip.exe
This will copy the encoded command to the clipboard, then all you need to do to use your command is type:
powershell.exe -EncodedCommand
.. and then paste in your command so that you end up with something like the following:
powershell.exe -EncodedCommand IAAgACMAIABFAG4AdABlAHIAIAB5AG8AdQByACAAcwBjAHIAaQBwAHQAIABjAG8AbgB0AGEAaQBuAGcAIABjAHUAcgBsAHkAIABiAHIAYQBjAGUAcwAgAGEAbgBkACAAcQB1AG8AdABlAHMACgAgACAAIwAgAEEAcwAgAGwAbwBuAGcAIABhAHMAIAB5AG8AdQAgAGwAaQBrAGUAIAAgAA==
You now have something that is command prompt safe.
Thanks Dave! Your solution give me an idea.
Here is the easiest solution to work on Jenkins.
I put the following scripts on a Powershell build step in my Jenkins build job.
$thisBuildInfoPath="$env:JOB_BUILD_DIR\ThisBuildInfo.md"
$fixedBugs=#("$env:BUILD_INFO_FIXED_BUGS")
(gc $thisBuildInfoPath) -replace "{{BUILD_INFO_FIXED_BUGS}}", $fixedBugs | sc $thisBuildInfoPath

Use double quote then curly brace in powershell -Command

There are a couple questions related to this on here but they specifically address Write-Host. I want to run something like
powershell.exe -Command "'example.exe' /f`"`{GUID`}`""
Only it fails with the error
Missing closing '}' in statement block.
At line:1 char:396
+ $mypid=(get-process EXEName*).id;wait-process -id $mypid;
& `C:\Users\User\AppData\Local\Temp\{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\Target.exe`
/s /ig``{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}` /instance=1 /f3`C:\Recordings`
/f4`uninstall-log.txt` /f1`C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}\setup.iss`
/f2`C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}\setup.log` /removeonly <<<<
+ CategoryInfo : ParserError: (CloseBraceToken:TokenId) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
I have other strings starting with " and containing {} (like /f"C:\Folder\{GUID}\program.exe" and these don't cause any trouble. It's only my argument where the curly braces are adjacent to the double quotes:
/ig`"`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}`"
In the error message, it may or may not be noteworthy that all the double quotes are gone so my /ig argument is left with two backticks. I believe my version is 2.0. Here is my actual (modified) command:
powershell -Command "$mypid=(get-process EXEName*).id;wait-process -id
$mypid;& 'C:\Users\User\AppData\Local\Temp\{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX}\Target.exe' /s /ig`"`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}`" /instance=1 /f3`"C:\Recordings`" /f4`"uninstall-log.txt`"
/f1`"C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}\setup.iss`"
/f2`"C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}\setup.log`" /removeonly"
Can anyone shed some light on this? I don't know why it would be invalid. Thank you!
The problem is using the -Command parameter for anything other than a simple script you will run into issues where characters such as curly braces and quotes will be misinterpritted by the command prompt before the are they are passed to Powershell. You could could tie yourself in knots by adding several layers of escaping or there is a simplier way - use the -EncodedCommand parametter instead.
For the EncodedCommand you just need to Base64 encode your command which you can do with the following Powershell script:
$command = #'
# Enter your commands containg curly braces and quotes here
# As long as you like
'#
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes) | clip.exe
This will copy the encoded command to the clipboard, then all you need to do to use your command is type:
powershell.exe -EncodedCommand
.. and then paste in your command so that you end up with something like the following:
powershell.exe -EncodedCommand IAAgACMAIABFAG4AdABlAHIAIAB5AG8AdQByACAAcwBjAHIAaQBwAHQAIABjAG8AbgB0AGEAaQBuAGcAIABjAHUAcgBsAHkAIABiAHIAYQBjAGUAcwAgAGEAbgBkACAAcQB1AG8AdABlAHMACgAgACAAIwAgAEEAcwAgAGwAbwBuAGcAIABhAHMAIAB5AG8AdQAgAGwAaQBrAGUAIAAgAA==
You now have something that is command prompt safe.