0
powershell "Start-Process -FilePath 'c:\PSexec.exe' -ArgumentList "-s -i -d -u .\USERNAME -p PASSWORD \\192.168.1.1 cmd /C "O: && cd O:\SOMEDIR && perl run.pl --socket 192.168.0.1:7890 & pause"" -Wait -Passthru -WindowStyle Hidden"

Launches a remote psexec process and uses powershell wrapper to wait for the process to finish.

It all works except when I add the powershell wrapper I can't seem to figure out how to correctly escape the internal double quotes.

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

'pause'`" -Wait -Passthru -WindowStyle Hidden"' is not recognized as an internal or external command,
operable program or batch file.

What I've Tried:

  1. I don't want to encode it into base64 using -EncodedString because the command will be called with some parts substituted potentially 1000s of times. Also this is being called from a perl script and don't want to have a powershell file that has to be checked for every time the perl script runs. (i.e. I would prefer to have to powershell command contained within the perl script)

  2. From How to escape powershell double quotes from a bat file? it suggests replacing double quotes with quadruple quotes. However when I do that I get

    powershell "Start-Process -FilePath 'c:\PSexec.exe' -ArgumentList ""-s -i -d -u .\USERNAME -p PASSWORD \\192.168.1.1 cmd /C ""O: && cd O:\SOMEDIR && perl run.pl --socket 192.168.0.1:7890 & pause"""" -Wait -Passthru -WindowStyle Hidden"
    
At line:1 char:1
+ Start-Process -FilePath 'c:\PSexec.exe' -ArgumentList -EncodedCommand ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
  1. ` escaping doesn't work because you need to escape the " from the command prompt so that it can be passed to powershell.
15
  • Why do you need to invoke powershell.exe at all? Commented Jan 19, 2018 at 21:03
  • As a wrapper so that the command called by psexec blocks until completion Commented Jan 19, 2018 at 21:04
  • Why not just run psexec.exe directly? Commented Jan 19, 2018 at 21:05
  • 1
    Perhaps you're asking a lot of XY Problem without realizing it? Commented Jan 19, 2018 at 23:14
  • 1
    @TheAschr, that wouldn't happen if you weren't trying to abuse PowerShell from within some other programming environment. It's not a loop-hole or short-cut out of your environment. Either write your scripts in PowerShell or use Perl or something else. Commented Jan 19, 2018 at 23:14

1 Answer 1

2

Drop the PowerShell call, it's not needed. Drop the -d option from the psexec invocation and it will wait for whatever process that it launched to complete. Read the psexec docs. From the docs we have:

-d Don't wait for process to terminate (non-interactive).

Sign up to request clarification or add additional context in comments.

3 Comments

The -d is being passed. It still exits prematurely. From above: 'c:\PSexec.exe' -ArgumentList "-s -i -d -u
Worked perfectly. Thank you.
You're welcome! Please take the time to edit your original question so that folks who actually need assistance with PowerShell quotes/escapes won't waste their time rolling in here.

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.