Skip to content

Instantly share code, notes, and snippets.

@craSH
Last active May 9, 2024 22:14
Show Gist options
  • Save craSH/c3821e1a053f617cae69487f1e1bbbf7 to your computer and use it in GitHub Desktop.
Save craSH/c3821e1a053f617cae69487f1e1bbbf7 to your computer and use it in GitHub Desktop.
PowerShell script that starts specified applications if they are not currently running. Useful in conjuncftion with Windows Event Log triggered Scheduled Tasks to provide a simple process supervisor system.
# Use this as the "Start Program" Action for an "On an event -> Log - > Application -> Application Error" Scheduled Task
# To automatically restart applications in the $monitored_apps hash if they crash (and the crash is reported in the event log)
#
# Setup scheduled task like documented @ https://www.reddit.com/r/windows/comments/4td55w/comment/d5gldsj/,
# except don't bother with Step 7 (keyword filtering) as it doesn't work as written there.
# Instead, delegate all process filtering to this script in the $monitored_apps hash below.
#
# Copyleft Ian Gallagher <crash@neg9.org>
# Applications to monitor and start if they are found to not be running (hash of Process fields Name and Path)
$monitored_apps = @{
'PDW' = 'C:\pdw\PDW.exe'
'SDR Console' = 'C:\Program Files\SDR-Radio.com (V3)\SDR Console.exe'
'ProScan' = 'C:\ProScan\ProScan.exe'
}
# Sleep momentarily in case the crashed application is lingering
Start-Sleep -Seconds 1.0
# Loop over each item in the monitored_apps table, check if it's running, if not, start it
$started_processes = @()
foreach ($app in $monitored_apps.GetEnumerator())
{
if (-Not (Get-Process | where name -eq $app.Key)) {
$started_processes += Start-Process -FilePath "$($app.Value)" -PassThru
}
}
foreach ($process in $started_processes) {
$msg = "Started application '$($process.Name)' ($($process.Path)) as it was not running."
Write-Host $msg
Write-EventLog -LogName Application -Source sdr-supervisor -EntryType Information -Category 0 -EventId 1 -Message $msg
}
if ($started_processes.Count -eq 0) {
$msg = "All expected applications are running ($($monitored_apps.Keys -Join ', '))"
Write-Host $msg
Write-EventLog -LogName Application -Source sdr-supervisor -EntryType Information -Category 0 -EventId 0 -Message $msg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment