Skip to content

maintenance_reboot.ps1

Corey Watson edited this page Dec 19, 2025 · 1 revision

maintenance_reboot.ps1

Forces a system reboot if uptime exceeds threshold OR if Windows reboot-pending flags are detected.

Overview

Property Value
Platform Windows
Version 7.2.1
Category Reboot & Recovery

Purpose

This script forces a system reboot if the current uptime exceeds a specified threshold in days OR if Windows reboot-pending flags are detected. It is designed as a routine maintenance task to ensure system stability and that pending updates are applied.

Requirements

  • PowerShell 5.1+
  • Must be run with local Administrator rights to query CIM and reboot

Inputs

Max Uptime Days:

  • RMM Name: $maxuptimedays (literal text placeholder)
  • Env Name: MAXUPTIMEDAYS
  • Constraints: Integer (1 or greater)
  • Default: 7

Note: This script is optimized for RMM environments using literal text replacement.

Data Sources & Priority

  1. RMM literal text replacement ($maxuptimedays)
  2. Environment variable (MAXUPTIMEDAYS)
  3. Default (7 days)

Settings (Hardcoded)

Setting Description Default
$DefaultMaxUptimeDays Default uptime threshold if RMM/env not set 7
$CheckCBSRebootPending Check Component Based Servicing flag $true
$CheckWURebootRequired Check Windows Update flag $true
$CheckPendingFileRename Check Pending File Rename Operations $true

Behavior

  • Retrieves the system's last boot time to calculate current uptime in days
  • Checks Windows registry for reboot-pending flags:
    • Component Based Servicing (CBS) RebootPending
    • Windows Update RebootRequired
    • Pending File Rename Operations (Session Manager)
  • Compares the current uptime against the resolved $MaxUptimeDays threshold
  • If uptime exceeds the threshold OR reboot flags are detected, a forceful reboot is initiated
  • If neither condition is met, a successful, non-action taken status is logged

Exit Codes

Code Description
0 Success (reboot not required or reboot initiated)
1 Failure (input validation failed or a command failed)

Notes

  • The reboot command is forceful (Restart-Computer -Force) and will not prompt users to save their work
  • This is by design for automated execution in RMM environments

Example Output

[ INPUT VALIDATION ]
--------------------------------------------------------------
Max Uptime Days          : 14
Check CBS Pending        : Yes
Check WU Required        : Yes
Check File Rename        : Yes

[ REBOOT FLAG CHECK ]
--------------------------------------------------------------
CBS RebootPending        : No
WU RebootRequired        : Yes
PendingFileRename        : No
Reboot Flags Detected    : Yes

[ UPTIME CHECK ]
--------------------------------------------------------------
Last Boot Time           : 2025-12-10T08:00:00
Current Uptime (Days)    : 5
Threshold (Days)         : 14
Uptime Exceeded          : No

[ REBOOT ACTION ]
--------------------------------------------------------------
Trigger                  : Reboot flags detected
Result                   : INITIATING REBOOT

[ FINAL STATUS ]
--------------------------------------------------------------
OPERATION COMPLETED SUCCESSFULLY (REBOOT INITIATED)

[ SCRIPT COMPLETED ]
--------------------------------------------------------------

Changelog

  • 2025-12-18 v7.2.1 Renamed from invoke_reboot_on_uptime.ps1 to maintenance_reboot.ps1
  • 2025-12-15 v7.2.0 Simplified configuration section to follow style guidelines
  • 2025-12-15 v7.0.0 Added reboot flag detection (CBS, Windows Update, PendingFileRename)
  • 2025-09-25 v6.0.0 Reverted conditional logic to robust IF/ELSEIF/ELSE structure
  • 2025-09-25 v5.0.1 Revised input resolution for RMM literal text replacement
  • 2025-09-25 v5.0.0 Code simplification using [WMI] accelerator
  • 2025-09-25 v4.0.0 Eliminated 'param()' keyword for RMM compatibility
  • 2025-09-05 v1.0.0 Initial script creation

Links

Clone this wiki locally