-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Restart-App.ps1
144 lines (120 loc) · 4.09 KB
/
Restart-App.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
<#
.SYNOPSIS
Restart the named process. This can be used to restart applications such as Outlook on a nightly
basis. Apps such as this tend to have memory leaks or become unstable over time when dealing with
huge amounts of data on a very active system.
.PARAMETER Arguments
A string specifying the command line arguments to pass to Command on startup.
.PARAMETER Command
The command to use to start the application.
If not provided then try to use the command line of the existing process.
This parameter is required when using the -Register switch.
.PARAMETER Name
The name of the process to restart.
.PARAMETER Register
If specified then register a Task Scheduler entry to run daily at 2am, for example:
Restart-App Outlook 'C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE' '/recycle' -register
.PARAMETER GetCommand
If specified then report the command line of the specified running process. This value can be
used to specify the Command parameter when registering.
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory = $true)] [string] $Name,
[string] $Command,
[string] $Arguments,
[switch] $Register,
[switch] $GetCommand
)
Begin
{
function GetCommandLine
{
$process = (Get-Process $Name -ErrorAction:SilentlyContinue)
if ($process -eq $null)
{
$script:cmd = $null
Write-Host "... process $Name not found"
}
else
{
# get the commandline from the process, strip off quotes
$cmd = (gwmi win32_process -filter ("ProcessID={0}" -f $process.id)).CommandLine
Write-Host "... found process $Name, ID $($process.ID), running $cmd"
}
}
function Shutdown
{
$process = (Get-Process $Name -ErrorAction:SilentlyContinue)
if ($process -ne $null)
{
# get the commandline from the process, strip off quotes
$script:cmd = (gwmi win32_process -filter ("ProcessID={0}" -f $process.id)).CommandLine.Replace('"', '')
Write-Host "... found process $Name running $cmd"
# terminating instead of graceful shutdown because can't connect using this:
# something funny about 32/64 or elevated process or just whatever
#$outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
Write-Host "... terminating process $Name"
$process.Kill()
$process = $null
Start-Sleep -s 10
}
else
{
Write-Host "... $Name process not found"
}
}
function Startup
{
if (!$cmd) { $cmd = $Command }
if (!$cmd)
{
Write-Host "*** No command specified to start $Name" -ForegroundColor Yellow
return
}
Write-Host "... starting $Name"
Write-Host "... $cmd $Arguments"
Invoke-Command -ScriptBlock { & $cmd $Arguments }
}
function RegisterTask
{
$cmd = "Restart-App -Name '$Name' -Command '$Command' -Arguments '$Arguments'"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am;
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-Command ""$cmd"""
$task = Get-ScheduledTask -TaskName "Restart $Name" -ErrorAction:SilentlyContinue
if ($task -eq $null)
{
Write-Host "... creating scheduled task 'Restart $Name'"
Register-ScheduledTask `
-Action $action `
-Trigger $trigger `
-TaskName "Restart $Name" `
-RunLevel Highest | Out-Null
}
else
{
Write-Host "... scheduled task 'Restart $Name' is already registered"
}
}
}
Process
{
if ($GetCommand)
{
GetCommandLine
return
}
if ($Register)
{
if (!$Command)
{
Write-Host '... Command argument is required when registering' -ForegroundColor Yellow
return
}
RegisterTask
return
}
Shutdown
Startup
}