无法使用 Python Pyautogui 以管理员身份打开 CMD

Unable to open CMD as admin using Python Pyautogui

我正在尝试使用 Python 3.7 pyautogui 以管理员身份打开 CMD。我能够导航到开始菜单图标,键入 'cmd' 并按 ctrl + shift +enter 以在管理员模式下打开 cmd。

然后会弹出一条消息,是或否是否以管理员身份打开。当我使用pyautogui.press('left')时,它没有按下左键。

try:
    import pyautogui
    import time
    pyautogui.FAILSAFE = True
    pyautogui.PAUSE = 0.5
    mouseMovementDuration = 2 #every mouse movement will take 2 secs
    intervalBetweenKeyPress = 0.5
    def runCMDasAdmin():
        x, y = pyautogui.locateCenterOnScreen(r'C:\Users\Saru\Desktop\PyAutoGUI\images\startmenu.png')
        pyautogui.click(x=x, y=y,button='left',duration=mouseMovementDuration) 
        pyautogui.typewrite('cmd', interval=intervalBetweenKeyPress)
        pyautogui.hotkey('ctrl', 'shift', 'enter')
        pyautogui.press(['left','enter'],interval=intervalBetweenKeyPress)

    print(pyautogui.size()) #It will give you the size of the screen
    pyautogui.moveTo(x=1919,y=1079,duration=mouseMovementDuration)
    pyautogui.moveTo(x=1,y=1,duration=mouseMovementDuration)
    runCMDasAdmin()
except Exception as e:
    print("Exception Raised------>",str(e))

我想使用 pyautogui 以管理员身份打开 cmd。请帮忙

您可以使用批处理脚本以管理员身份打开CMD。 下面是代码。

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

由于用户访问控制 (UAC) 提示在独立层上工作,不受任何 automation/code 组件的控制,以确保用户安全。所以唯一的解决办法就是完全禁用 UAC 提示。禁用 UAC 提示的过程 - 在开始菜单中键入 UAC,select 用户访问控制设置,将其设置为从不通知。