1

I need to run a script that starts a service in python. The problem is that when the command is run in cmd, an UAC window appears. I have found some solutions:

  1. The easiest one is changing UAC settings so it does not appear. However, I would like to find a way not to have to change my computer settings, since this is dangerous and also we would have to change it in every local computer we might run the script from.
  2. I found a way to create a task with high administrator rights which means I wouldn't have to change UAC settings, but still the task should be created in all machines.
  3. I also tried this How to run python script with elevated privilege on windows fix and it works, but the UAC window keeps appearing.

I have even tried clicking on the "Yes" button, but the problem here is that the script stops the moment UAC message appears. I appreciate any ideas on the problem! If I found a way to keep running the script while the message is open, I could easily click "Yes" and that would solve it.

This is the script that I want to run:

import datetime
import win32api, win32con
import sys, os, traceback, types

def run(test, browserName = None):
    import win32com.shell.shell as shell
    commands = 'python F:\service.py'
    shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)

and in F:\service.py:

import win32serviceutil
serviceName = "myService"
win32serviceutil.StopService(serviceName)

#From here on is where I try of clicking "Yes" which is not working
x = 953 
y = 639
click(x,y)

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
Diana Vallverdu
  • 321
  • 4
  • 13
  • 3
    Easy enough to start another script in the background, but it won't help: the UAC dialog is protected, you can't interfere with it unless you already have admin privileges. Have you considered changing the permissions on myService so that it can be stopped without needing admin privilege? – Harry Johnston Aug 24 '17 at 22:58
  • 1
    Starting/stopping a service requires admin rights. Launching `cmd` with `runas` will prompt and block the caller until permission is granted. You can't avoid that if the script is not running elevated. If you don't want to block the script, call `ShellExecuteEx` in a separate thread. – Remy Lebeau Aug 25 '17 at 06:22
  • "Starting/stopping a service requires admin rights"--yes, usually, unless you change the access control for the service to allow some other principal to start and stop it, as Harry Johnston already mentioned. – Bill_Stewart Sep 12 '17 at 19:02

0 Answers0