How to set-up a scheduled device restart using PowerShell with a pop-up notification.
Pending reboot devices are always a challenge for IT Professional to meet all the demands associated with SLAs compliance . This article explains how to Schedule your computer to reboot at a certain time using PowerShell script and an options for the end-user to defer the restart. We have alternate options to perform a scheduled restart if you are using any Client Management applications or any other third party solutions where the cost is involved. Here I’m explaining a simple device restart solution that can be achieved through a Task scheduler and PowerShell Script. GPO is a prerequisites here to push the PowerShell script and create a task scheduler.
As per the example provided below we are setting the device to restart every 5 days, restart interval can be easily modified in the script based on the requirement. All the pop-up windows for the users are configured in the visual studio (WPF). The configuration XAML will be then imported to the PowerShell to get the GUI option for the end-user. The current GUI is designed to look like MECM default restart pop-up notification windows.
Below are the users experience while running the Pop-Up notification. We can edit the notification content from XAML code in the script and increase the notification count if required
First Notification for the user.
This notification will appear on the user’s device on N-1 day of the actual reboot configured day. If reboot is configured with a 5 days interval, the first notification will start from 4th day. Number of pop-up count per day can be defined in the task scheduler trigger option.
Users can either SNOOZE the notification or restart the device
Second Notification for the user
This notification will appear on the user’s device based on the reboot interval configured in the PowerShell script. If the reboot interval is configured to reboot the device every 5 day, the second popup will appear on the 5th day with a one hour SZOOZE time for the users.
User can either SNOOZE the notification for the next one hour or restart the device to avoid any data lose during the force restart.
Final Notification for the user
This notification is the final one for the users after the 1 hour SZOOZE time from the previous notification. Users gets a 5-minute time before the computer restarts to save all the work and avoid data lose.
This time Users must restart the computer and there is no option to SNOOZE OR Close button option on the window. Once the countdown reaches to Zero device gets restarted forcefully.
Now we will move to the scripting part of this solution
Below are the important PowerShell cmdlet used to restart the device.
Set the PowerShell variables.
- RestartDuration – We can set the restart duration in this variable, 5 days is the default restart interval in this script
- OrganizationName – This is the Name that shown in the Pop-Up windows title.
########## Set restart duration Variable ############################################ $Global:RestartDuration = 5 #System reboots every 5 days $Global:OrganizationName = "WWW.VIVEKRR.COM" $Global:DayBefore = $RestartDuration - 1 #####################################################################################
Get the Last system reboot information.
Below PowerShell cmdlet is used to get the last system reboot information. The notification and device restart happens based on this date.
$computerOS = Get-CimInstance CIM_OperatingSystem $lastreboot=$computerOS.LastBootUpTime.ToString("yyy-MM-dd") $today=(get-date).ToString("yyy-MM-dd") $TimeSpan = [DateTime]$today - [DateTime]$lastreboot; $TimeSpan.Days
Defining the logic of restart based on the last reboot information
Here we can define the script logic for restarting the device based on the last reboot information. If you want to increase the popup count for users you can define here.
if($days -eq 0) { Write-Host "No Action" } elseif ($Days -eq $DayBefore) { RestartTomorrow } elseif (($days -gt $RestartDuration) -or ($days -eq $RestartDuration)) { RestartOneHour Get-Date start-sleep -Seconds 3300 $Exithour = 1 if ($Exithour -eq 1) { Forcerestart start-sleep -Seconds 5 #Restart-Computer -Force } }
Important Note I have commented the Force Restart option defined after the one hour Szooze time, however if you click on the Restart Now button from notification window device gets restarted forcefully. Please test the script in the LAB infra before you try to execute on production device to avoid any force restart due to the wrong update in the script.
Download the full script from my GitHub repository mentioned in the below this article.
Setup GPO to Create the Task scheduler and push the PowerShell script to client device.
Below are the important steps that we need to follow while creating the GPO. The GPO always should run under the user context to get the PowerShell notification. Also, sign the script if your Infar’s PowerShell execution policy is restricted to run the PowerShell script.
- Open GPO Console and and expand domain controller – Go to Group Policy Object and Click on New
- Go to User Configuration – Preferences – Control Panel Settings – Scheduled Tasks
- Right Click on Scheduled Tasks and Go to New- Scheduled Task ( At least Windows 7)
The General Tab setting should be as follows – As mentioned earlier task scheduler should run with user rights.
The Action Tab setting should be with the below settings-
Action : Start a program
Program/Script : %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments : -WindowStyle Hidden -ExecutionPolicy Bypass -file “C:\Program Files\ DeviceReboot.ps1”
Now create an another policy form the same GPO to transfer the PowerShell script. Make sure that we are copying the PowerShell script to the same device location which we defined the PowerShell Argument in the previous screenshot (“C:\Program Files\ DeviceReboot.ps1”)
- Edit the existing GPO again
- Go to Computer Configuration – Preferences – Windows Settings – Files and click on New-File
Set the Source and Destination location as shown below. Make sure that destination location should be same as our PowerShell Argument’s location (“C:\Program Files\ DeviceReboot.ps1”)
Great content! Keep up the good work!