1

I am trying to create batch file to automate Sysinternals execution. This will eventually be executed by a Python script to automatically elevate credentials of a program (yes, I know this is bad practice, yes, there were ways to supply the password to "runas", etc, etc, but none of these solutions have worked for me)

When I open a command prompt as a regular user and type the following

.\psexec \\my_IP_address -u DOMAIN\my_admin_account -p my_admin_password cmd

I get

PsExec v2.0 - Execute processes remotely
Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com


Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>

From there I can type in

.\psloggedon -l -x \\ip_address_of_remote_computer

And the screen prints the result

PsLoggedon v1.34 - See who's logged on
Copyright (C) 2000-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

Users logged on locally:
        DOMAIN\last_user_logged_in

But when I try to create the following batch file

cd pstools
.\psexec \\my_IP_address -u DOMAIN\adminaccount -p adminpasword cmd
cd pstools
.\psloggedon -l -x \\ip_address_of_remote_computer

And when I execute the batch file, it only executes the first two commands

cd pstools
.\psexec \\my_IP_address -u DOMAIN\adminaccount -p adminpasword cmd

How do I get it to execute all of the commands?

In affect, I am opening a command prompt and THEN elevating privileges (which is something I plan to incorporate into a script)

Glowie
  • 2,271
  • 21
  • 60
  • 104

1 Answers1

0

This is because your psloggedon command runs in the original cmd, not the new one.

You should pass what you want to be run as a parameter for cmd. For example, this worked for me:

psexec cmd /c "whoami & pause"

So you should also do something similar, e.g.:

cd pstools
.\psexec \\my_IP_address -u DOMAIN\adminaccount -p adminpasword cmd /c "cd pstools & psloggedon -l -x \\ip_address_of_remote_computer"

Another option, especially useful if the internal logic grows more complicated, is to create a new batch file just for that, and pass it as a parameter to cmd to run.

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31