- 1 year ago
Demoting a subprocess call from running as admin
When you make a subprocess call in Python, it inherits the permissions of the parent process. If the parent process is running with administrative privileges, the subprocess will also run with administrative privileges by default. However, if you want to demote the subprocess to run without admin privileges, you can use the runas
Windows command and specify a non-administrative user.
Here's an example of how you can demote a subprocess call to run without admin privileges in Python:
pythonimport subprocess
# Command to be executed by the subprocess
command = ['runas', '/user:NonAdminUser', 'python', 'your_script.py']
# Execute the subprocess with the specified command
subprocess.run(command)
In the above code, replace 'NonAdminUser'
with the username of a non-administrative user on your system. The runas
command allows you to run a command as a different user. By specifying a non-administrative user, the subprocess will run without administrative privileges.
Additionally, you need to provide the command you want to execute after the runas
command. In the example above, it executes the python
command and runs a script called your_script.py
. Modify it according to your needs.
Please note that when using runas
, you may be prompted to enter the password for the specified user.