Geo V
Geo V

Reputation: 151

PSEXEC Starts on remote PC but does not execute command

I have a command to execute on the remote PC on an interactive session to sent a HotKey to the console. The remote command is PowerShell

$wsshell = New-Object -comObject wscript.shell
$wsshell.SendKeys("+{Q}")

When I run the psexec on the remote work station - locally, it does send the Shift+Q key and run another application without any issue

However when I run remotely via an interactive session with -i 2 (2 being session which I can see on the console) with -s and -h (calling user and remote logged user are administrator on the remote PC)

When I execute the psexec remotely, I can interactively see that the powershell is getting executed (can see .bat file console on the remote PC) but it does not send the Shift+Q to run the other application.

Any help appreciated - can it be related to Powershell Execution Policy. The Execution Policy is RemoteSigned

SMBShare, Fileshare and Print rule is in place for Domain and enabled

Upvotes: 1

Views: 948

Answers (1)

mklement0
mklement0

Reputation: 439812

Note:

  • The following is based on my own experiments with psexec as of v2.43, and the findings are mostly not spelled out in the linked documentation.
    • Also - as of this writing - a recent update to that documentation makes an incorrect claim about -i being required for interactive console applications / shells with redirected in- and output (where the console window on the caller side acts like a terminal for the remotely executing program). Unfortunately, the example calls were also updated based on this misconception.

I conclude from the following experiment that what you're trying to do can not be done, as of psexec v2.43, and as tried on both a Windows 10 and a Windows 11 machine:

The following juxtaposes a local invocation using a simple test command with a remote one; the test command tries to use -i in combination with -s to run a PowerShell command visibly on the target machine, which, after a delay of 3 seconds sends keystrokes (exit + Enter) to its own window to auto-close it:

  • The local call - which doesn't have a \\<computer> argument - does work:
# OK, but only without \\<machine>
psexec -i -s -nobanner powershell -executionpolicy bypass -noprofile -noexit -c '$wsshell = New-Object -comObject wscript.shell; $wsshell.SendKeys(''exit{ENTER}''); ''Sending keystrokes to exit this session in 3 secs...''; Start-Sleep 3'
  • By contrast, even though adding "\\$env:COMPUTERNAME" combined with -i alone seems to work (at least when "remoting" to the same machine), it is the addition of -s that breaks the call:

    • Adding -s seemingly causes the -i option to be ignored, so that the command runs invisibly on the target machine, with its stdout output getting captured and relayed to the caller.

    • That said, this question implies that at least at some point in the past combining -i and -s (as well as -i and -u, which my experiments suggest also no longer works) did work and, per your feedback, still does work for you in v2.43, and that the only problem you saw was that the keypress-sending part of the interactive remote execution didn't work - if anyone has any insights, please let us know.

# NOT OK, due to combination of \\<machine> with -s 
# (without -s, i.e. with ONLY -i, it works, at least on the same machine)
psexec "\\$env:COMPUTERNAME" -i -s -nobanner powershell -executionpolicy bypass -noprofile -noexit -c '$wsshell = New-Object -comObject wscript.shell; $wsshell.SendKeys(''exit{ENTER}''); ''Sending keystrokes to exit this session in 3 secs...''; Start-Sleep 3'

Upvotes: 0

Related Questions