Batch file to automate Sysinternals execution

China☆狼群 提交于 2019-12-23 03:27:17

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/23785165/batch-file-to-automate-sysinternals-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!