variable containing command:

set code=$param3 = 'https://edmullen.net/test/rc.jpg'; $c = '$task = $client.GetByteArrayAsync(""' + $param3; $d = '$task = $client.GetByteArrayAsync(""' + $param3;

When I run both CMD codes below, I get different results on each time for both, how?

start powershell -noexit "%code%"
start powershell -noexit "!code!"

Results of $c & $d: (notice the quote mark)

PS > $c
$task = $client.GetByteArrayAsync("https://edmullen.net/test/rc.jpg
PS > $d
$task = $client.GetByteArrayAsync(https://edmullen.net/test/rc.jpg
PS >

Found it!
Seems to be some powershell bug: The grave accent ` or double quote " to escape a double quote " resulted in the inconsistencies. Using backslash \ to escape works in both cases.

Since I'm calling the powershell code from Win CMD, Microsoft support states to use caret ^ as escape character.
See similar question here:

In this instance, the caret ^ doesn't work to escape the quote either.

set code=$param3 = 'https://edmullen.net/test/rc.jpg'; $d = '$task = $client.GetByteArrayAsync(\"' + $param3 + '\"'; $c = '$task = $client.GetByteArrayAsync(\"' + $param3 + '\"';

Results in:

PS > $c
$task = $client.GetByteArrayAsync("https://edmullen.net/test/rc.jpg"
PS > $d
$task = $client.GetByteArrayAsync("https://edmullen.net/test/rc.jpg"
PS >

As a last resort, I would have had to run encoded command as stated in help file:

Accepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces.

To use the -EncodedCommand parameter:

$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.