powershellpsexec

PSEXEC Not executing command when password with " is specified


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.


Solution

  • 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:


    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