TheAschr
TheAschr

Reputation: 973

Can't find syntax error in double quoted powershell command

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.

Upvotes: 0

Views: 233

Answers (1)

jwdonahue
jwdonahue

Reputation: 6669

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).

Upvotes: 2

Related Questions