Uptime Remediation Script Explained
The uptime remediation script is designed to display a toast notification on Windows systems that have not performed a reboot in a certain amount of time. The script checks the system's uptime and compares it to a threshold value, then displays a toast notification prompting the user to restart their device.
Prerequisites
Before running this script, you need to: * Ensure PowerShell is installed and configured correctly on your Windows machine * Make sure you have the necessary permissions to create registry entries and files
The Script
The script consists of several sections that perform specific tasks. Here's a breakdown of each section:
function Display-ToastNotification() {
$Load = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$Load = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
# Load the notification into the required format
$ToastXML = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXML.LoadXml($Toast.OuterXml)
# Display the toast notification
try {
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml)
}
catch {
Write-Output -Message 'Something went wrong when displaying the toast notification' -Level Warn
Write-Output -Message 'Make sure the script is running as the logged on user' -Level Warn
}
}
This section defines a function called `Display-ToastNotification` that creates a toast notification using the Windows.UI.Notifications namespace. The function takes no parameters and returns no value.
$Hour = (Get-Date).TimeOfDay.Hours
if ($Hour –ge 0 –and $Hour –lt 12){
$Greeting = "Good Morning"
}
elseif ($Hour –ge 12 –and $Hour –lt 16){
$Greeting = "Good Afternoon"
}
else{
$Greeting = "Good Evening"
}
#$GivenName = Get-GivenName
$Greeting = $Greeting
This section determines the current hour of the day and sets a greeting message based on the time. The greeting message is used to personalize the toast notification.
$classpath = "Registry::HKCR:\"
$keyname = "MSUptimeToastReboot"
$keyDefault = "url:MSUptimeToastReboot"
$keyeditflags = "2162688"
$valcontent = 'C:\Windows\System32\shutdown.exe -r -t 60 -c "Your computer will be restarted in 1 minute, please save any work you have open."'
if (!(test-path "HKCR:\")) {
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR
}
set-location -path HKCR:\
$classcheck = Get-ChildItem -Path "HKCR:\$($keyname)\" -Recurse -ErrorAction SilentlyContinue
if (!($classcheck)) {
#create class protocol
new-item -Path "HKCR:\\" -Name $keyname -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "(Default)" -PropertyType "string" -Value $keydefault -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "EditFlags" -PropertyType "DWORD" -Value $keyeditflags -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "URL Protocol" -PropertyType "string" -Value "" -Verbose
new-item -Path "HKCR:\\$keyname" -Name "Shell" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell" -Name "Open" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell\Open" -Name "command" -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname\Shell\Open\command" -name "(Default)" -PropertyType "string" -Value $valcontent -Verbose
}
Remove-PSDrive -Name HKCR -Force
set-location c:\windows\system32
This section creates a registry key and sets the necessary values to create a class protocol. The script also checks if the registry key exists and creates it if it doesn't.
# Setting image variables
$LogoImageUri = [your logo here]
$HeroImageUri = [your hero here]
$LogoImage = "$env:TEMP\ToastLogoImage.png"
$HeroImage = "$env:TEMP\ToastHeroImage.png"
$Uptime = get-computerinfo | Select-Object OSUptime
#Fetching images from uri
Invoke-WebRequest -Uri $LogoImageUri -OutFile $LogoImage
Invoke-WebRequest -Uri $HeroImageUri -OutFile $HeroImage
This section sets the image variables for the toast notification and fetches the images from the specified URIs.
#Defining the Toast notification settings
#ToastNotification Settings
$Scenario = 'reminder' #
# Load Toast Notification text
$AttributionText = "[Your company here]"
$HeaderText = "$($greeting), a reboot is required!"
$TitleText = "Your device has not performed a reboot in the last $($Uptime.OsUptime.Days) days"
$BodyText1 = "For performance and stability reasons we suggest a reboot at least once a week."
$BodyText2 = "Please save your work and restart your device at the earliest convenience."
# Check for required entries in registry for when using Powershell as application for the toast
# Register the AppID in the registry for use with the Action Center, if required
$RegPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings'
$App = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
# Creating registry entries if they don't exists
if (-NOT(Test-Path -Path "$RegPath\$App")) {
New-Item -Path "$RegPath\$App" -Force
New-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD'
}
# Make sure the app used with the action center is enabled
if ((Get-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -ErrorAction SilentlyContinue).ShowInActionCenter -ne '1') {
New-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD' -Force
}
This section sets the toast notification settings and loads the necessary text. It also checks if the registry key exists and creates it if it doesn't, and makes sure the app is enabled for use with the action center.
# Formatting the toast notification XML
[xml]$Toast = @"
$AttributionText
$HeaderText
$TitleText
$BodyText1
$BodyText2
"@
#Send the notification
Display-ToastNotification
Exit 0
This section formats the toast notification XML and sends it using the `Display-ToastNotification` function. That's a summary of the uptime remediation script. The script checks system uptime, sets greeting messages based on time, creates registry keys, fetches images, sets toast notification settings, and sends the notification.