menu

Questions & Answers

I need to automate running commands as admin in windows command prompt using python

here is the task, I have a command which has to run as admin in command prompt using python. Entire process has be automated without user interface. Using the ctype library, I can run Python commands on the command line as administrator. The issue is that it asks for a Username and password while the command is being executed. Is it possible to send these inputs to the command prompt?

this is the code snippet:

import ctypes
cmd1 = u'/k pysv_checkout -l common -b -ru'
ctypes.windll.shell32.ShellExecuteW(None, u"runas", u"cmd.exe",cmd1,None,1) 

have attempted to use the subprocess library, but I am unable to do so as an administrator or get it to communicate with the command prompt even after using subprocess.communicate()

Comments:
2023-01-24 00:00:02
Run the command from a script that is executed with a service account that has administrator privilege. Or you can use the subprocess library to run the command, and then use the communicate() method to send the inputs to the command prompt.
2023-01-24 00:00:02
You can't elevate a process to admin if the process that's starting the process doesn't have admin already (unless you get a user to provide credentials at runtime, which may be tricky). It's not impossible to enter the credentials programmatically, but it's a bad idea and would involve storing the credentials in your code (and thus, essentially in plain text). If you care that little about security, you may as well run your script as admin.
Answers(0) :