'Run a program' option in windows service pannel for failure recovery

1.6k views Asked by At

I am trying to run a perl script whenever there is a service crash. The perl script intends to restart the service and send a mail to all the developers.

I have used windows recovery options for that, where it has an option to run a program . I have filled the required details in the command line option but the script doesn't seem to get executed. Can you please help me by sharing your knowledge on this?

Recovery tab configuration

I have tried with Restart service option and that is working fine but the run a program isn't executing the script. Am I missing something? Any comment on this will be helpful.

1

There are 1 answers

0
Nick On

I recently implemented a recovery option to run a powershell script that attempts to restart the service a defined number of times and sends an email notification at the conclusion, it also attaches a txt file with recent relevant logs.

After several attempts (and despite all the other things I have seen) The configuration of fields on the recovery tab in services is as follows:

Program: Powershell.exe
**Not C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe

Command line parameters: -command "& {SomePath\YourScript.ps1 '$args[0]' '$args[1]' '$args[n]'}"

eg: -command "& {C:\PowershellScripts\ServicesRecovery.ps1 'Service Name'}"

**The $args are parameters that will be passed to your script. These are not required.

here is the powershell script:

cd $PSScriptRoot

$n = $args[0]

function CreateLogFile {
$events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
if (!(Test-Path "c:\temp")) {
    New-Item -Path "c:\temp" -Type directory}
if (!(Test-Path "c:\temp\ServicesLogs.txt")) {
    New-Item -Path "c:\temp" -Type File -Name "ServicesLogs.txt"}
    $events | Out-File -width 600 c:\temp\ServicesLogs.txt
}

function SendEmail  {
$EmailServer = "SMTP Server"
$ToAddress = "[email protected]"
$FromAddress = "[email protected]"

CreateLogFile

$Retrycount = $Retrycount + 1
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
-Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:\temp\ServicesLogs.txt

Remove-Item "c:\temp\ServicesLogs.txt"
}

function SendEmailFail  {
$EmailServer = "SMTP Server"
$ToAddress = "[email protected]"
$FromAddress = "[email protected]"

CreateLogFile

$Retrycount = $Retrycount + 1
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
-Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:\temp\ServicesLogs.txt

Remove-Item "c:\temp\ServicesLogs.txt"
}

function StartService {

$Stoploop = $false

do {
   if ($Retrycount -gt 3){
     $Stoploop = $true
     SendEmail
     Break
    }

   $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
    if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {

        sc.exe start $n
        Start-Sleep -Seconds 35

        $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
          if ($i.state -eq "Running"){
              $Stoploop = $true
              SendEmailFail}
          else {$Retrycount = $Retrycount + 1}
    }        
}
While ($Stoploop -eq $false)
}

[int]$Retrycount = "0"
StartService