Issue:
PSEXEC Executed from PS Script not running the command passed as argument when -p is specified along with a password that contains "
.
Code:
$x = read-host -prompt 'Enter something:'
PSEXEC -u storeadmin -p ('"' + ($x -replace '"', '""') + '"') \\srXXX01 cmd /c TIME /T
Result:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
Expectation:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
XX:XX <-- THE ACTUAL TIME, then EXIT as I've invoked /C in CMD
Note:
This occurs only when password has "
like Anything:";K
. I've tested this on 100 different store machines. Only the stores with "
has this abnormal behavior
Issue Details: It exhibits the same behavior even when entered in the CLI on both Powershell and CMD directly.
Update:
The issue appears only when there's a " on the password. When there's a ", it just executes the cmd
and not include /C TIME /T
. I don't know why.
Update
It sounds like the issue is with psexec
itself, whose command line parsing may be broken with passwords containing embedded "
instances - see this ancient blog post - sounds like it never got fixed.
Your - suboptimal - options are:
Change the password to not include double quotes (which may not be a bad idea in general, as other utilities may have trouble with such passwords too).
Use the workaround suggested here: since the problem appears to be with passing additional parameters, put the command line a batch file - if the parameters vary, you can create this batch file dynamically and copy it to the remote machine for execution with psexec
's -c
option.
Below is the original answer, demonstrating various methods to escape double quotes.
The question, which originally contained a multi-statement approach to escaping, was later updated with one of these methods.
If psexec
expects embedded double quotes to be represented as ""
, do the following:
$x = read-host -prompt 'Enter something:'
PSEXEC -u storeadmin -p $($x -replace '"', '""') \\srXXX01 cmd /c TIME /T
With an input of g=76"&,;Jl
, psexec
will literally be passed g=76""&,;Jl
.
If psexec
ends up "eating" the embedded ""
, try the following:
PSEXEC -u storeadmin -p ('"' + ($x -replace '"', '""') + '"') \\srXXX01 cmd /c TIME /T
If psexec
requires "
to be escaped as \"
(which would be unusual for an MS program, which typically at least also support ""
), try:
PSEXEC -u storeadmin -p ('"' + ($x -replace '"', '\"') + '"') \\srXXX01 cmd /c TIME /T