In Windows, I am using SC.EXE
to stop, uninstall, reinstall and restart a service after an update-deployment. Now I need to change the on-error-behavior of a windows service only using cmd (I want the service to restart on error). This can easily be set using the services GUI interface but is there a way to access these settings without using the gui? Powershell is also OK but I'd prefer to stick to simple CMD. Any suggestions?
Asked
Active
Viewed 1,055 times
0

Eric Leschinski
- 146,994
- 96
- 417
- 335

MichaelS
- 5,941
- 6
- 31
- 46
-
4possible duplicate of [Install Windows Service with Recovery action to Restart](http://stackoverflow.com/questions/1633429/install-windows-service-with-recovery-action-to-restart). Have a look at one of the answers that is using SC.exe to configure the restart actions – Matt Oct 21 '14 at 13:38
1 Answers
-1
You can use the -ErrorAction action tag for generalized error solutions.
Stop-Process -Name "" -Value Stop -ErrorAction SilentlyContinue
Or if it's a warning you can use -WarningAction with the same usage method.
You can also use the -ErrorVariable to store errors produced throughout the running process in a string variable.
Stop-Process 123 -ErrorVariable a
$a
That will run a process and put all error output into var "a" as a string.
If Your Wanting Something To Act On Behalf Of The Errors Here's a Simple Script That Should Do What You Want It To Do:(The "#" indicates notes I put in there for you)
# Gathers Service To Be Altered
$serviceSum = @("", "")
# Goes Through Array And Set's All Specified Services To Restart Upon Failure
function Set-ServiceErrorAction([ref]$serviceSum)
{
foreach($serviceInst in $serviceSum)
{
$services = Get-WMIObject win32_service | Where-Object {$_.description -imatch "$serviceInst" -and $_.startmode -eq "Auto"};
foreach($service in $services)
{
sc.exe failure $service.name reset= 86400 actions= restart/5000
}
}
}
Set-ServiceErrorAction
Enjoy,

CalebB
- 597
- 3
- 17
-
this is not an answer to the question the op asked, sorry. Matt´s comment links the correct answer. – Paul Oct 21 '14 at 14:16
-
@Paul I edited my answer to more aptly apply to what he was wanting to do. My first answer was more applying to the general ways powershell can react to errors. – CalebB Oct 21 '14 at 14:43
-
i dont think the op wants to start the service from powershell every time which would be required for your script to work or am i wrong with this? (also stopping services with unvalidated userinput is extremely irresponsible :)). Nonetheless i appreciate your work – Paul Oct 21 '14 at 14:51
-
@Paul That's Why I put the Note "#Action To Try" meaning whatever action he wants to trying involving services, it's a template for him to build off, eg. adding more inputted services and using an array, or whatever the op would like to do. If he wants to integrate the script into the service installation and distribution we would need to add a little more in. – CalebB Oct 21 '14 at 15:02
-
I see where you are going with this and like i said i appreciate it. I´m still not sure though if this is useful for the action the op wants to take since your script is not able to automatically react to a failing service, only to errors while starting and stopping. Have you taken a look at the link Matt provided in his comment? – Paul Oct 21 '14 at 15:08
-
1@Paul You are correct, my apologies. I posted new code that will go through all the services specified and set all their error action types to restart when there's an error. That should be more apt to what he is looking for. No I did not see his comment. I'll check that out. – CalebB Oct 21 '14 at 15:52