我正在尝试以管理员身份使用Python 3.7 pyautogui打开CMD。我可以导航到开始菜单图标,键入' cmd‘,然后按ctrl + shift +enter在管理模式下打开cmd。
然后弹出一条消息,指出是否以管理员身份打开。当我使用pyautogui.press(‘左’)时,它没有按左按钮。
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。请帮帮忙。
发布于 2019-02-17 13:20:09
因为用户访问控制(UAC)提示符在独立的层上工作,而不受任何自动化/代码组件的控制,以确保用户的安全。因此,唯一的解决方案是完全禁用UAC提示。禁用UAC提示的过程-在开始菜单上键入UAC,选择用户访问控制设置,将其设置为从不通知。
发布于 2019-02-06 01:52:58
您可以使用批处理脚本以管理员身份打开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"
:--------------------------------------
https://stackoverflow.com/questions/54540047
复制相似问题