即使在让 Python 通过函数获得提升的访问权限之后,通过 Python 启用 Windows 功能也不会让 DISM 获得提升的访问权限
我正在尝试通过 Python 函数启用 Windows 功能。这是我使用过的脚本:
UAC.py:
def gainadminaccess():
import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
if __name__ == "__main__":
gainadminaccess()
winfeatures.py
def install(feature):
packageName = feature
import os
os.system("cmd /k dism /online /Enable-Feature /FeatureName:" + packageName + "/All")
if __name__ == "__main__":
install()
enablefeatures.py:
import UAC
import winfeatures
import os
UAC.gainadminaccess()
winfeatures.install('VirtualMachinePlatform')
我可以通过 UAC 提示,但是当通过 winfeatures 到达功能部分时,它将显示:
Error: 740
Elevated permissions are required to run DISM.
Use an elevated command prompt to complete these tasks.
我认为 DISM 没有无法获得更高的访问权限。有什么解决办法吗?
I'm trying to enable Windows Features through Python through functions. Here's the scripts that I've used:
UAC.py:
def gainadminaccess():
import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
if __name__ == "__main__":
gainadminaccess()
winfeatures.py
def install(feature):
packageName = feature
import os
os.system("cmd /k dism /online /Enable-Feature /FeatureName:" + packageName + "/All")
if __name__ == "__main__":
install()
enablefeatures.py:
import UAC
import winfeatures
import os
UAC.gainadminaccess()
winfeatures.install('VirtualMachinePlatform')
I'm OK with passing the UAC prompt, but when arriving at the feature part through winfeatures, it will show:
Error: 740
Elevated permissions are required to run DISM.
Use an elevated command prompt to complete these tasks.
which I assume that DISM didn't get elevated access. Any fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这:
第一行将启动一个新进程。第二行执行,但它仍然以非管理员身份运行:
我想你想要这个:
然后在你的其他文件中:
This:
The first line will launch a new process. The second line executes but it is still running as non-admin:
I think you want this instead:
Then in your other file: