-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestartThatApp.ps1
149 lines (131 loc) · 5.34 KB
/
RestartThatApp.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#requires -Version 3
Clear-Host
<#
Author: Blackkatt
Version: 1.1.3
Name: RestartThatApp
Purpose:
Restart an faulting V.I.P. application.
Instructions:
Please run 'RestartThatApp.cmd' to install.
What Will Happen:
If not present, 'RestartThatApp.xml' is created & imported in Task Scheduler under "Event Viewer Tasks\RestartThatApp"
this task triggers on an (Event 1000, Application Error) and will restart faulting app if found on the VIP List.
Note:
If you move 'RestartThatApp.ps1' after installation task will stop working. Until you update the task with the new path.
The VIP List:
Applications on this list will be restarted if they crash. Use format below to add/remove to/from the list.
#>
$TheVIPList = '(deluge.*|kodi.*|notepad.*)$'
#region CUSTOMIZE
$TaskExist = 'Task Already Exist, Moving On!'
$TaskMissing = "Task Doesn't Exist, Creating!"
$Events = 'Querying Event Log. . .'
$Whitelisted = 'has crashed and is on the VIP list.'
$Blacklisted = 'has crashed but NOT on the VIP list. . .'
$RestartThatApp = "Locating Application. . .`nrestarting"
$Done = "I'm done here! thanks for playing."
#endregion CUSTOMIZE
#region CREATE TASK
$GetTask = Get-ScheduledTask -TaskPath '\Event Viewer Tasks\' -TaskName RestartThatApp -ErrorAction SilentlyContinue
if($GetTask)
{
Write-Output -InputObject $TaskExist # Don't Create Task
}
else
{
Write-Output -InputObject $TaskMissing # Create Task
$template = @"
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2016-06-25T15:44:21.9111474</Date>
<Author>BK\Authority</Author>
<Description>Restarts crashed application if not found on the The VIP List</Description>
<URI>\Event Viewer Tasks\RestartThatApp</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="Application"><Select Path="Application">*[System[Provider[@Name='Application Error'] and EventID=1000]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-545</GroupId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1M</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments></Arguments>
<WorkingDirectory>C:\Windows\System32\WindowsPowerShell\v1.0</WorkingDirectory>
</Exec>
</Actions>
</Task>
"@
$varFile = $template | Tee-Object -Variable RestartThatApp.xml
# Manipulate XML Content.
Set-Location $PSScriptRoot
$varFile = 'Variable:\RestartThatApp.xml'
$xmlFile = "$pwd\RestartThatApp.xml"
$ps1File = @"
"$pwd\RestartThatApp.ps1"
"@
[xml]$getXML = Get-Content $varFile
$getXML.Task.Actions.Exec.Arguments = "-NonInteractive -NoLogo -NoProfile -WindowStyle Hidden -ExecutionPolicy ByPass -File $ps1File"
# Export Task.
$getXML.Save($xmlFile)
# Import Task.
schtasks.exe /create /tn 'Event Viewer Tasks\RestartThatApp' /XML $xmlFile
}
#endregion CREATE TASK
#region GET-EVENT LOG
$GetEvent = Get-WinEvent -ProviderName 'Application Error' -MaxEvents 1
$EventMessage = $GetEvent.Message
$AppPath = [regex]::Match($EventMessage,'(Faulting application path: )(.:\\.*\.exe)').Groups[2].Value
$AppPathSplit = $AppPath.Split('\')
$AppName = $AppPathSplit.Item(2)
#endregion GET-EVENT LOG
#region RESTART THAT APP
$EP = {Stop-Process -Name $AppName -Force -ErrorAction SilentlyContinue}
$SP = {Start-Process -FilePath $AppPath}
$WP = {Wait-Process -Name $AppName -Timeout 5 -ErrorAction SilentlyContinue}
if ($AppName -Match $TheVIPList)
{
$Report = "`n`n$Events`n$AppName $Whitelisted`n`n$RestartThatApp $AppName ($AppPath)`n`n$Done"
Write-Output -InputObject "$Report"
Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 300 -EntryType Information -Message "$Report" -Category 1 -RawData 10, 20
&$EP
&$WP
&$SP
}
else
{
$Report = "`n`n$Events`n$AppName $Blacklisted $Done"
Write-Output -InputObject "$Report"
Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 300 -EntryType Information -Message "$Report" -Category 1 -RawData 10, 20
}
#endregion RESTART THAT APP