在 Python 中将管理员权限设置为 subprocess.check_call() [英] Set administrator privileges to subprocess.check_call() in Python

查看:64
本文介绍了在 Python 中将管理员权限设置为 subprocess.check_call()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 python 脚本中调用了两个可执行文件,一个需要管理员权限,另一个不需要它们.有什么方法可以在执行可执行文件之前设置管理员权限,这样它就不会要求我输入密码.

I call two executables from a python script one which needs Administrator privileges and one which does not need them. Is there any way by which I can set the Administrator privilege before executing the executable so that it does not ask me for my password.

我的脚本如下

import subprocess

subprocess.check_call(['DoesnotNeedAdminPrivilege.exe'])
subprocess.check_call(['NeedsAdminPrivilege.exe'])

我尝试通过启动 cmd 以管理员身份运行来从 cmd 运行此脚本.有什么方法可以将这些管理员权限传递给第二个可执行文件,使其正常工作

I tried to run this script from cmd by starting the cmd to run as an Adminstrator. Is there any way to pass these admin rights to the second executable so that it works without any problem

推荐答案

我发现了一个讨厌的解决方法

I found a nasty workaround

    f = open('test.cmd', 'w+')
    f.write("execute.exe")
    f.close()

    os.system("runas /savecred /profile /user:Administrator \"test.cmd\"")

或者你可以使用子进程

这篇关于在 Python 中将管理员权限设置为 subprocess.check_call()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆