How to run an application(.exe) as a different user by using python script

1.9k Views Asked by At

I am trying to run an exe file as different user from python script. I tried using Subprocess, using "runas" cmd it fails to receive the password from communicate("password") or from stdin. Here is the part of code for subprocess,

Tried this:

p = subprocess.Popen("runas /user:.\\admin cmd.exe").communicate("abc12346")

and this:

p1 = subprocess.Popen("echo abc123456",shell=True,stdout=subprocess.PIPE)
p2 = subprocess.Popen("runas /user:.\\admin cmd.exe",shell=True,stdin=p1.stdout).communicate()

Both are giving error, Error

Enter the password for MyPC\admin:
Attempting to start cmd.exe as user "MyPC\admin" ...
RUNAS ERROR: Unable to run - cmd.exe
1326: Logon failure: unknown user name or bad password.

Note: The password that I have given is correct.

Please let me know how to run it as different user from using python script. Note: user could be from any group or local users.

0

There are 0 best solutions below