Reboot Device

SOTI MobiControl
Windows

As organizations become more application-centric, corporate devices must manage an increasing number of applications. However, higher app usage can strain Random Access Memory (RAM), affecting device performance. Restarting a Windows device helps clear RAM, enhance performance, apply pending OS updates, and install software patches. It also serves as a troubleshooting method. Admins can use SOTI MobiControl to deploy a custom script that schedules a remote device restart after a specified period.

Important:
  • It is recommended to test the script on a local/ test machine for its purpose and effects. 
  • SOTI MobiControl will not be responsible for any damage/loss to the data/setup based on the behavior of the script.

Reboot device

1 $folderName = "RestartScripts"
2 $programFilesPath = "C:\Program Files"
3 $fileName = "UptimeNew.ps1"
4 $fileContent = @'
5 $restartPath = "C:\Program Files\RestartScripts\RestartPrompt.ps1"
6 $forceRestartPath = "C:\Program Files\RestartScripts\ForceRestart.ps1"
7 $os = Get-CimInstance -ClassName Win32_OperatingSystem
8 $lastBootTime = $os.LastBootUpTime
9 $uptime = (Get-Date) - $lastBootTime
10 $totalMinutes = [math]::Round($uptime.TotalMinutes)
11 $quserOutput = quser
12 $activeUser = $quserOutput | ForEach-Object {
13 if ($_ -match '^\s*(\S+)\s+(\S+)\s+\S+\s+Active\s') {
14 $matches[1]
15 }
16 }
17 if ($activeUser.StartsWith(">")) {
18 $activeUser = $activeUser.Substring(1)
19 }
20 function RestartScheduledTask {
21 param (
22 [string]$ScriptPath
23 )
24 $taskName = "RestartPrompt"
25 $taskDescription = "Windows Prompt for Restarting device"
26 $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -File `"$ScriptPath`" -mins $totalMinutes"
27 $startTime = (Get-Date).AddMinutes(1)
28 $trigger = New-ScheduledTaskTrigger -Once -At $startTime
29 $principal = New-ScheduledTaskPrincipal -UserId "$activeUser" -LogonType Interactive
30 $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
31 $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
32 if ($existingTask) {
33 Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
34 }
35 Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description $taskDescription -Principal $principal -Settings $settings
36 }
37 switch ($totalMinutes) {
38 {$_ -ge 10 } {
39 RestartScheduledTask -ScriptPath $forceRestartPath
40 break
41 }
42 {$_ -ge 5 -and $_ -lt 6} {
43 RestartScheduledTask -ScriptPath $restartPath
44 break
45 }
46 {$_ -ge 3 -and $_ -lt 4} {
47 RestartScheduledTask -ScriptPath $restartPath
48 break
49 }
50 }
51 '@
52 $newFolderPath = Join-Path -Path $programFilesPath -ChildPath $folderName
53 if (-Not (Test-Path -Path $newFolderPath)) {
54 New-Item -Path $newFolderPath -ItemType Directory
55 Write-Output "Folder '$folderName' created successfully at '$programFilesPath'."
56 } else {
57 Write-Output "Folder '$folderName' already exists at '$programFilesPath'."
58 }
59 $newFilePath = Join-Path -Path $newFolderPath -ChildPath $fileName
60 Set-Content -Path $newFilePath -Value $fileContent
61 Write-Output "File '$fileName' created successfully at '$newFolderPath'."
62 $fileName = "RestartPrompt.ps1"
63 $fileContent = @'
64 param(
65 [int]$mins
66 )
67 Add-Type -AssemblyName PresentationFramework
68 $result = [System.Windows.MessageBox]::Show("Uptime of $mins minutes reached. Do you want to restart the machine?", "Restart Machine", [System.Windows.MessageBoxButton]::YesNo)
69 if ($result -eq [System.Windows.MessageBoxResult]::Yes) {
70 Restart-Computer -Force
71 } else {
72 [System.Windows.MessageBox]::Show("Restart canceled.", "Restart Machine", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Information)
73 }
74 '@
75 $newFolderPath = Join-Path -Path $programFilesPath -ChildPath $folderName
76 if (-Not (Test-Path -Path $newFolderPath)) {
77 New-Item -Path $newFolderPath -ItemType Directory
78 Write-Output "Folder '$folderName' created successfully at '$programFilesPath'."
79 } else {
80 Write-Output "Folder '$folderName' already exists at '$programFilesPath'."
81 }
82 $newFilePath = Join-Path -Path $newFolderPath -ChildPath $fileName
83 Set-Content -Path $newFilePath -Value $fileContent
84 Write-Output "File '$fileName' created successfully at '$newFolderPath'."
85 $fileName = "ForceRestart.ps1"
86 $fileContent = @'
87 Add-Type -AssemblyName PresentationFramework
88 Start-Job -ScriptBlock {
89 Start-Sleep -Seconds 5
90 Restart-Computer -Force
91 }
92 $result = [System.Windows.MessageBox]::Show(
93 "The device will restart in 5 seconds. Uptime exceeding beyond acceptable time. Click OK to restart now",
94 "Restart Machine",
95 [System.Windows.MessageBoxButton]::OK,
96 [System.Windows.MessageBoxImage]::Information
97 )
98 if ($result -eq [System.Windows.MessageBoxResult]::OK) {
99 Restart-Computer -Force
100 }
101 '@
102 $newFolderPath = Join-Path -Path $programFilesPath -ChildPath $folderName
103 if (-Not (Test-Path -Path $newFolderPath)) {
104 New-Item -Path $newFolderPath -ItemType Directory
105 Write-Output "Folder '$folderName' created successfully at '$programFilesPath'."
106 } else {
107 Write-Output "Folder '$folderName' already exists at '$programFilesPath'."
108 }
109 $newFilePath = Join-Path -Path $newFolderPath -ChildPath $fileName
110 Set-Content -Path $newFilePath -Value $fileContent
111 Write-Output "File '$fileName' created successfully at '$newFolderPath'."
112 $scriptPath = "C:\Program Files\RestartScripts\UptimeNew.ps1"
113 $taskName = "fetchUptime"
114 $taskDescription = "FetchUptime"
115 $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -NoProfile -File `"$scriptPath`""
116 $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Days 365)
117 $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM"
118 $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
119 Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description $taskDescription -Principal $principal -Settings $settings

Was this helpful?

Need more help?
Ask Community