即使在让 Python 通过函数获得提升的访问权限之后,通过 Python 启用 Windows 功能也不会让 DISM 获得提升的访问权限

发布于 2025-01-14 05:58:47 字数 1234 浏览 5 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2025-01-21 05:58:47

这:

UAC.gainadminaccess()
winfeatures.install('VirtualMachinePlatform')

第一​​行将启动一个新进程。第二行执行,但它仍然以非管理员身份运行:

我想你想要这个:

def gainadminaccess():
    import os
    import sys
    import win32com.shell.shell as shell
    ASADMIN = 'asadmin'

    isAdmin = sys.argv[-1] == ASADMIN

    if not isAdmin:
        script = os.path.abspath(sys.argv[0])
        params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
        shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    
    return isAdmin # returns true if this script is already running as admin

然后在你的其他文件中:

if UAC.gainadminaccess():
    winfeatures.install('VirtualMachinePlatform')

This:

UAC.gainadminaccess()
winfeatures.install('VirtualMachinePlatform')

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:

def gainadminaccess():
    import os
    import sys
    import win32com.shell.shell as shell
    ASADMIN = 'asadmin'

    isAdmin = sys.argv[-1] == ASADMIN

    if not isAdmin:
        script = os.path.abspath(sys.argv[0])
        params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
        shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    
    return isAdmin # returns true if this script is already running as admin

Then in your other file:

if UAC.gainadminaccess():
    winfeatures.install('VirtualMachinePlatform')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文