I want to write a piece of code to be run on windows that checks if a service is running and if it is not, start it.
I have a timer that every minute check for the status of the service.
I use this code to do so:
strCommand = “sc query MyService”
s = sh.Result
i = InStr(s, “RUNNING”)
if i <> 0 then
where StartService is a method with this code inside:
Declare Function ShellExecuteW Lib “Shell32” (HWND As Integer, verb As WString, file As WString, params As WString, directory As WString, cmd As Integer) As Integer
Call ShellExecuteW(0, “runas”, “ActiveService.bat”, " ", “C:\MyFolder”, 1)
and ActiveService.bat is in C:\MyFolder with this DOS command inside:
sc start MyService
Works fine BUT Windows, ask for confirmation.
Is there any known way to avoid human this confirmation in order to have an automatic process ??
The easiest method would be to run your program elevated as administrator but most end users wont like that, if its just an internal app that might be the easiest option.
The human confirmation is to stop nefarious programs doing things on your computer without your knowledge. What you’re trying to do isn’t really the correct way to do things, this is an old method of doing things and while isn’t not impossible to do there are security implications and things to do to make this work.
Instead, services have a failure recovery system built in so you can have it automatically restart itself after x time, you can even have it do things like try a different method of restart on 2nd failure and a completely different system on subsequent fails like run an app you’ve made that will “call for assistance”.
This can all be set up using the sc failure command when you install the service in the first place (which you needed elevation for).
As this app will run on a server and only administrators will have access I feel that if running the app as administrator solves the problem I will go in this way.
I will try and if it works I will tell you.
Hi @ … running the app as administrator solves the problem. Thanks
I will try now your second suggestion: the comand sc failure
I’m glad it worked.
If its just a single install and you’re ok to config it by hand you can alter the failure settings by right clicking properties on the service in the services viewer and selecting the Recovery tab. If the tab is greyed out, you’ll need to run the services as administrator which is usually visually done these days by typing services into search and right clicking the entry “Run as administrator”.
The runas verb triggers UAC.
Is that intentional? You could use open instead.