-
Notifications
You must be signed in to change notification settings - Fork 9
/
restart-computer.ps1
48 lines (38 loc) · 1.4 KB
/
restart-computer.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
# Scriptoteket reboot script for kiosk screens
# Exits Powerpoint and Chrome gracefully before rebooting.
# Author: Dan Michael
# Modified: 2019-07-04
# ------------------------------------------------------------------------------------
# The following method works gracefully even if document recovery is visible
Try {
$app = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Powerpoint.Application")
Write-Host "Trying to close Powerpoint gracefully..."
$app.DisplayAlerts = $false
$app.Quit()
Wait-Process -Name POWERPNT -ErrorAction SilentlyContinue -Timeout 4
Start-Sleep -Seconds 1
} Catch {}
$apps = @("POWERPNT", "chrome")
foreach ($app in $apps) {
# Is it running?
$appRunning = Get-Process $app -ErrorAction SilentlyContinue
if ($appRunning) {
# Try closing it gracefully first.
$appRunning | Foreach-Object {
Write-Host "Trying to close $app (process $($_.Id)) gracefully..."
$_.CloseMainWindow() | Out-Null
}
Wait-Process -Name $app -ErrorAction SilentlyContinue -Timeout 4
Start-Sleep -Seconds 1
# If still running, kill it.
Get-Process $app -ErrorAction SilentlyContinue | Foreach-Object {
Write-Host "Sending stop signal to process $($_.Id)"
Stop-Process $_ -Force | Out-Null
}
Wait-Process -Name $app -ErrorAction SilentlyContinue -Timeout 4
Start-Sleep -Seconds 1
}
}
Write-Host "Restarting computer"
Start-Sleep -Seconds 10
Restart-Computer -Force