How to securely use PSEXEC with a remote user and password from a batch file?

Taed Wynnell 0 Reputation points
2024-07-18T02:53:39.1+00:00

I use PSEXEC to administer many embedded Windows systems (no KVM) that are not part of our domain. (Think of a thermostat or freezer.) They use their own user/password that does not exist in our domain or locally. I use "PSEXEC -u user -p password" to connect to them without a problem.

The IT group pointed out that the command line is logged (I assume process auditing in Windows Security Event Log) and so they see the user/password and don't like the security aspect of that, which is fine.

However, I can't figure out a way around that for my use.

Yes, if I don't specify the "-p password", it will prompt me for a password. However, I want to do this from the command line for many machines, so I don't want to continually retype the password.

I tried to ECHO the password and pipe it into PSEXEC, but that surprisingly didn't work. (I assume that PSEXEC flushes the input before prompting for a password.) Similarly, I put the password in a file and redirected it into PSEXEC and that similarly did not work.

I also tried to NET USE the remote \C$, \ADMIN$, and \IPC$ shares, thinking PSEXEC would not then need a user/password, but nope. It seems like the \ADMIN$ should have worked.

I understand that PSEXEC version 2.1 and up sends the password over the network encrypted. I verified it myself, so I'm satisfied with that aspect of it.

So does anyone know how I can use PSEXEC from a batch file to connect to a user/password on another machine without specifying the password on the command line?

Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,169 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2024-07-19T09:18:28.3966667+00:00

    There may be an easier way but using a launcher process to tweak the PsExec64 command line worked in a test for me. The launcher process starts PsExec64.exe suspended. The command line passed to CreateProcess uses "XXXXXXXX" for the user and password parameters. This is what is captured in the Security event log for Process Creation event id 4688. The launcher process replaces the filler characters with the real user name and password and then resumes the PsExec64.exe process.

    Event log entry -

    Auditlog

    Its hackish and ugly but it worked.

    1 person found this answer helpful.

  2. MotoX80 34,431 Reputation points
    2024-07-20T17:36:20.76+00:00

    What OS are these machines running? How about using Powershell instead of psexec? On the remote systems run "winrm quickconfig" to enable remoting.

    $target = "test10"
    $script = 
     {
        "This script is running on {0}" -f $env:COMPUTERNAME
        ""
        "**** Calling whoami to execute on the remote system"
        c:\Windows\system32\whoami.exe
        ""
     }
     $User = ".\admin"
     $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force
     $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    Invoke-Command -ComputerName  $target -credential $Credential -ScriptBlock $script
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.