1

I am trying to open CMD as admin using Python 3.7 pyautogui. I am able to navigate to the start menu icon, type 'cmd' and press ctrl + shift +enter for opening cmd in admin mode.

Then a pop-up message comes up with yes or no whether to open as admin or not. When I am using pyautogui.press('left'), it is not pressing the left button.

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))

I want to open cmd as admin using pyautogui. Please help.

  • 1
    Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – hegash Feb 05 '19 at 17:40
  • If I'm not mistaken, those UAC windows are opened on a separate layer that usually disables other programs' influences to prevent malicious codes. Therefore once the UAC pops up you don't have control over the screen/keyboard any more. You might want to use `subprocess` instead, see relevant answer here: https://stackoverflow.com/questions/47380378/run-process-as-admin-with-subprocess-run-in-python?rq=1 – r.ook Feb 05 '19 at 18:16

2 Answers2

1

As the User Access Control (UAC) prompt works on a separate layer beyond the control of any automation/code component for user's security. So the only solution is to disable UAC prompts completely. The procedure to disable UAC prompts - On start menu type UAC, select User Access Control settings, set it to never notify.

0

You can use batch script to open CMD as Admin. Below is the code.

@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"
:--------------------------------------
Naveen
  • 610
  • 11
  • 21
  • Sorry but, your code is creating multiple instances of command prompt and goes on endlessly and neither of them is opening 1 cmd with administrative privileges. I used os.system(BatchFilePath) to run the batch file. – Sarthak Mahapatra Feb 09 '19 at 04:43
  • I see the issue here. this code is to start cmd with admin privileges from a bat file. If you want to open a cmd from python, you got to start the python with admin privileges first and that will make whatever you are opening have the same privileges. In order to achieve that you can add the code from this [answer](https://stackoverflow.com/a/33856172/7964299) in your Python script. – Naveen Feb 09 '19 at 16:22