Created
September 20, 2018 09:24
-
-
Save daBONDi/2e92e74a5fdd194a015ac2aa21ec8beb to your computer and use it in GitHub Desktop.
Start an Application with a Powershell Script an Restart it Automatic on non Return Code 0, also capture Eventlog crash entry into the log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start the Terminal Application on Logon | |
# Automatic Restart Terminal Application on Crash | |
Param([String]$Path, [String]$ProcessName="MenuePlan.Terminal.UI", [String]$LogFile="generateme") | |
$defaultLogRootPath = "c:/temp"; # Define the Default Directory for Log Files | |
$InstantCrashCounter = 5; # Ammount of Instant Crashes before we stop restarting Application | |
$secondForDetectingInstantCrash = 60; # Define when a Instant Crash happen in Seconds | |
if($Path -eq "") | |
{ | |
Write-Error -Message "Path is required!" -ErrorAction Stop | |
} | |
if(-not (Test-Path -Path $Path)) | |
{ | |
Write-Error -Message "Cannot find Executable on $Path" -ErrorAction Stop | |
} | |
if($LogFile -eq "generateme") | |
{ | |
$logFileName = "menuplan_starter_" + $Path.Replace('/','-').Replace(':','-').Replace('\','-').Replace('.','_') + ".log"; | |
$LogFile = "$($defaultLogRootPath)/$($logfileName)" | |
} | |
function Add-Log($Message,$Sub="global",$WithoutStamp=$false) | |
{ | |
$logTime = Get-Date -Format "dd-MM-yyyy hh-mm-ss" | |
$entry = "{0} : {1}`t{2}" -f $logTime,$Sub,$Message; | |
if($withoutStamp) | |
{ | |
$entry = "`t`t`t`t{0}" -f $Message; | |
} | |
Write-Host $entry; | |
$entry | Out-File $LogFile -Append -Force -Encoding utf8; | |
} | |
function Get-ApplicationCrash($Path, $ProcessName, $After) | |
{ | |
$applog = Get-EventLog -LogName Application -EntryType Error -Newest 1 -After $After | Where-Object { $_.Message -like "*$($Path)*" } | Select -ExpandProperty Message | |
if($applog -ne "") | |
{ | |
Add-Log -Message "Application Crash Event Log" -Sub "Crash" | |
foreach($line in $($applog -split "`r`n")) | |
{ | |
Add-Log -Message $line -WithoutStamp $true | |
} | |
} | |
} | |
function StartApp($Path,$ProcessName) | |
{ | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
$pinfo.FileName = "$Path" | |
$pinfo.RedirectStandardError = $true | |
$pinfo.RedirectStandardOutput = $true | |
$pinfo.UseShellExecute = $false | |
$pinfo.Arguments = "" | |
$p = New-Object System.Diagnostics.Process | |
$p.StartInfo = $pinfo | |
Add-Log -Message "Starting Application $($ProcessName)" -Sub "Watcher"; | |
$p.Start() | Out-Null | |
#Do Other Stuff Here.... | |
$p.WaitForExit() | |
$p.ExitCode | |
} | |
# MAIN | |
do | |
{ | |
$StartDate = Get-Date; | |
$returnCode = StartApp -Path $Path -ProcessName $ProcessName; | |
if($returnCode -ne 0) | |
{ | |
Get-ApplicationCrash -Path $Path -ProcessName $ProcessName -After $StartDate; | |
Add-Log -Message "Application $($ProcessName) has Invalid Return Code $($returnCode)" -Sub "Crash"; | |
Add-Log -Message "Restarting Application $($ProcessName)" -Sub "Watcher"; | |
# Detect if Application Crash Instantly | |
if( $($StartDate.AddSeconds($secondForDetectingInstantCrash)) -gt $(Get-Date) ) { | |
# We Crashed Instantly | |
$InstantCrashCounter--; | |
Add-Log -Message "Application $($ProcessName) Crash Instantly. CrashCounter: $($InstantCrashCounter)/5" -Sub "Crash"; | |
}else{ | |
$InstantCrashCounter=5; | |
} | |
# Detect if we Crash to often | |
if($InstantCrashCounter -eq 0) | |
{ | |
Add-Log -Message "Application $($ProcessName) crash to often, we stopping" -Sub "Crash"; | |
$host.SetShouldExit($returnCode); | |
exit | |
} | |
} | |
}until($returnCode -eq 0); | |
Add-Log -Message "Application $($ProcessName) stop successfully with Exit Code $returnCode" -Sub "Watcher" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment