def copypasta(action, params, wait_state, doing_verb, done_verb): 'common code in a lot of methods here :)' try: target = params[0] # Do the requested action. status = System.get_service(target) try: name = System.get_service_display_name(target) except WindowsError: name = target print "%s service \"%s\"..." % (doing_verb, name) action(*params) # Wait for it to finish. timeout = 20 status = System.get_service(target) while status.CurrentState == wait_state: timeout -= 1 if timeout <= 0: print "Error: timed out." return time.sleep(0.5) status = System.get_service(target) # Done. print "Service %s successfully." % done_verb # On error show a message and quit. except WindowsError, e: print str(e) return
def restart_service( service ): try: # Get the display name. try: display_name = System.get_service_display_name( service ) except WindowsError: display_name = service # Get the service descriptor. descriptor = System.get_service( service ) # Is the service running? if descriptor.CurrentState != win32.SERVICE_STOPPED: # Tell the service to stop. print "Stopping service \"%s\"..." % display_name System.stop_service( service ) # Wait for the service to stop. wait_for_service( service, win32.SERVICE_STOP_PENDING ) print "Service stopped successfully." # Tell the service to start. print "Starting service \"%s\"..." % display_name System.start_service( service ) # Wait for the service to start. wait_for_service( service, win32.SERVICE_START_PENDING ) print "Service started successfully." # Show the new process ID. # This feature requires Windows XP and above. descriptor = System.get_service( service ) try: print "New process ID is: %d" % descriptor.ProcessId except AttributeError: pass # On error, show an error message. except WindowsError, e: if e.winerror == win32.ERROR_ACCESS_DENIED: print "Access denied! Is this an UAC elevated prompt?" else: print str(e)