Skip to main content

Server Reboot Schedule on Specific Days

Summary

This document outlines the process for scheduling a forced reboot of the server on specific days based on the approved window check.

Sample Run

Select the device and search for 'Workstation Re' in the search box, then click.

Sample Run Step 1

Provide the user parameter value as per the requirement and then click 'Continue to scheduling'.

Sample Run Step 2

Click 'Run task' to schedule the task immediately.

Sample Run Step 3

User Parameters

NameExampleRequiredDescription
HoursToReboot4TrueThe hour of the day to reboot in a 24-hour format. Example: 13 = 1:00 PM
MinutesToReboot0TrueThe minute of the hour to reboot. Supports values from 0 - 59
DaysToReboot'Tuesday','Wednesday','Thursday'TrueMust be a comma-separated list of strings in single quotes with the names of the days of the week to reboot. Example: 'Tuesday','Wednesday','Thursday'

Dependencies

Custom Fields - Reboot Trigger/Schedule

Implementation

Create Task

To implement this script, please create a new "Script Editor" style script in the system.

Create Task

  • Name: Server Reboot Schedule on Specific Days
  • Description: This schedules the forced reboot of the server on the scheduled days based on the approved window check.
  • Category: Maintenance

Create Task Details

Script

Start by making three separate rows. You can do this by clicking the "Add Row" button at the bottom of the script page.

Add Row

Row 1: Function: Script Log

Row 1

Paste the highlighted text: Checking OS

Row 1 Detail

Row 2: Function: PowerShell Script

Row 2

Row 2 Detail

Paste in the following PowerShell script and set the expected time of script execution to 600 seconds.

$os = Get-CimInstance -ClassName Win32_OperatingSystem
if ($os.ProductType -eq 1) {
return 'Workstation'
} elseif ($os.ProductType -eq 2 -or $os.ProductType -eq 3) {
return 'Server'
} else {
return 'Unknown OS Type'
}

Row 3: Function: Script Log

Row 3

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 3 Detail

Row 4: Logic: If/Then

Row 4

Row 4a: Condition: Output Contains

In the IF part, enter Server in the right box of the "Output Does Not Contain" part.

Row 4a

Row 4b: Function: Script Log

Row 4b

In the script log message, type The OS detected is confirmed not a Server. Please try the script 'Force Reboot Workstation with Reboot Windows Verification'. Exiting script without any action. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 4b Detail

Row 4c: Function: Script Exit

Add a new row by clicking on the Add Row button.

Row 4c

In the script exit message, leave it blank.

Row 4c Detail

Row 5: Logic: If/Then

Row 5

Row 5a: Condition: Custom Field Equals

In the IF part, enter true in the right box of the "Custom Field Equals" part.

Row 5a

Row 5b: Function: Script Log

Row 5b

In the script log message, type Force Reboot is approved on the endpoint via Reboot Scripts. Proceeding for reboot schedule without reboot window validation. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 5b Detail

Row 5c: Function: PowerShell Script

Row 5c

Row 5c Detail

Paste in the following PowerShell script and set the expected time of script execution to 900 seconds.

function Restart-ComputerOnSchedule {
[CmdletBinding()]
param (
[Parameter(Mandatory)][string[]]$Days,
[Parameter(Mandatory)][int]$Hour,
[Parameter(Mandatory)][int]$Minute,
[switch]$Test
)
if(-not [bool]$Days) {
Write-Output "ERROR: No days specified to reboot."
return
}
$INCORRECT_DAYS = $Days | Where-Object {$_ -notmatch "^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)$"}
if([bool]$INCORRECT_DAYS) {
Write-Output "ERROR: Specified days are incorrect: $($INCORRECT_DAYS -join ',')"
return
}
if((0..23) -notcontains $Hour) {
Write-Output "ERROR: Specified hour is incorrect: $Hour"
return
}
if((0..59) -notcontains $Minute) {
Write-Output "ERROR: Specified minute is incorrect: $Minute"
return
}
$currentDate = Get-Date
$rebootDate = $(Get-Date -Hour $Hour -Minute $Minute).AddDays(1)
$failsafe = 0
while($Days -notcontains $rebootDate.DayOfWeek) {
if($failsafe -gt 7) {break}
$rebootDate = $rebootDate.AddDays(1)
$failsafe++
}
if($failsafe -gt 7) {
Write-Output "ERROR: Something went wrong and the reboot date could not be determined."
return
}
$rebootDelay = [Math]::Round($($rebootDate - $currentDate).TotalSeconds)
$rebootComment = "Server is being rebooted automatically at: $($rebootDate.tostring())"
$rebootArgs = @("/r", "/t", $rebootDelay, "/c", $rebootComment)
if(-not $Test) {
& shutdown /a 2>&1 | Out-Null
& shutdown $rebootArgs 2>&1 | Out-Null
}

Write-Output "RebootDateTime=$($rebootDate.tostring("yyyy-MM-dd hh:mm:ss"))"
}
Restart-ComputerOnSchedule -Days (@DaysToReboot@) -Hour @HoursToReboot@ -Minute @MinutesToReboot@

Row 5d: Function: Script Log

Row 5d

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 5d Detail

Row 5e: Logic: If/Then

Row 5e

Row 5e.1: Condition: Output Does Not Contain

In the IF part, enter RebootDateTime in the right box of the "Output Does Not Contain" part.

Row 5e.1

Row 5e.2: Function: Script Exit

Row 5e.2

In the script exit message, type Reboot Schedule Failed with an error %output%. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device. This will end the task with failure.

Row 5e.2 Detail

Row 5f: Function: Script Log

Row 5f

In the script log message, type Reboot successfully scheduled on %output%. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 5f Detail

Row 5g: Function: Script Exit

Add a new row by clicking on the Add Row button.

Row 5g

In the script exit message, leave it blank.

Row 5g Detail

Row 6: Function: Set Pre-defined Variable

Row 6

Select the custom field 'ServerRebootWindowStart' and set the variable 'RebootWindowStart'.

Row 6 Detail

Row 7: Function: Set Pre-defined Variable

Row 7

Select the custom field 'ServerRebootWindowEnd' and set the variable 'RebootWindowEnd'.

Row 7 Detail

Row 8: Function: Set Pre-defined Variable

Row 8

Select the custom field 'ServerRebootWindowDay' and set the variable 'RebootWindowDay'.

Row 8 Detail

Row 9: Logic: If/Then

Row 9

Row 9a: Condition: Custom Field Equals

In the IF part, enter true in the right box of the "Custom Field Equals" part.

Row 9a

Row 9b: Function: Script Log

Row 9b

In the script log message, type This endpoint has 'Exclude Reboot' flag checked. Exiting script without any action. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 9b Detail

Row 9c: Function: Script Exit

Add a new row by clicking on the Add Row button.

Row 9c

In the script exit message, leave it blank.

Row 9c Detail

Row 10: Function: Script Log

Row 10

In the script log message, type Verifying that machine is in approved window between @RebootWindowStart@ and @RebootWindowEnd@ (Note: 0=12:00 AM, 23=11:00 PM) and Day @RebootWindowDay@. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 10 Detail

Row 11: Function: PowerShell Script

Row 11

Row 11 Detail

Paste in the following PowerShell script and set the expected time of script execution to 600 seconds.

$hour = [int](get-date -Format "HH");
$currentDay = (get-date).DayOfWeek
$Day = @RebootWindowDay@
$minHour = @RebootWindowStart@
$maxHour = @RebootWindowEnd@
if ($Day -contains $currentDay) {
if (($hour -ge $minHour) -and ($hour -lt $maxHour)) {
return 'Machine is in reboot window'
}
else {
return 'Time is not in approved window'
}
}
else {
return 'Day is not in approved window'
}

Row 12: Function: Script Log

Row 12

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 12 Detail

Row 13: Logic: If/Then

Row 13

Row 13a: Condition: Output Contains

In the IF part, enter Machine is in reboot window in the right box of the "Output Does Not Contain" part.

Row 13a

Row 13b: Function: Script Log

Row 13b

In the script log message, type Scheduling the reboot as the reboot schedule falls under the reboot window set on the company level. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 13b Detail

Row 13c: Function: PowerShell Script

Row 13c

Row 13c Detail

Paste in the following PowerShell script and set the expected time of script execution to 900 seconds.

function Restart-ComputerOnSchedule {
[CmdletBinding()]
param (
[Parameter(Mandatory)][string[]]$Days,
[Parameter(Mandatory)][int]$Hour,
[Parameter(Mandatory)][int]$Minute,
[switch]$Test
)
if(-not [bool]$Days) {
Write-Output "ERROR: No days specified to reboot."
return
}
$INCORRECT_DAYS = $Days | Where-Object {$_ -notmatch "^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)$"}
if([bool]$INCORRECT_DAYS) {
Write-Output "ERROR: Specified days are incorrect: $($INCORRECT_DAYS -join ',')"
return
}
if((0..23) -notcontains $Hour) {
Write-Output "ERROR: Specified hour is incorrect: $Hour"
return
}
if((0..59) -notcontains $Minute) {
Write-Output "ERROR: Specified minute is incorrect: $Minute"
return
}
$currentDate = Get-Date
$rebootDate = $(Get-Date -Hour $Hour -Minute $Minute).AddDays(1)
$failsafe = 0
while($Days -notcontains $rebootDate.DayOfWeek) {
if($failsafe -gt 7) {break}
$rebootDate = $rebootDate.AddDays(1)
$failsafe++
}
if($failsafe -gt 7) {
Write-Output "ERROR: Something went wrong and the reboot date could not be determined."
return
}
$rebootDelay = [Math]::Round($($rebootDate - $currentDate).TotalSeconds)
$rebootComment = "Server is being rebooted automatically at: $($rebootDate.tostring())"
$rebootArgs = @("/r", "/t", $rebootDelay, "/c", $rebootComment)
if(-not $Test) {
& shutdown /a 2>&1 | Out-Null
& shutdown $rebootArgs 2>&1 | Out-Null
}

Write-Output "RebootDateTime=$($rebootDate.tostring("yyyy-MM-dd hh:mm:ss"))"
}
Restart-ComputerOnSchedule -Days (@DaysToReboot@) -Hour @HoursToReboot@ -Minute @MinutesToReboot@

Row 13d: Function: Script Log

Row 13d

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 13d Detail

Row 13e: Function: Script Log

Row 13e

Row 13e Detail

Row 13e.1: Condition: Output Contains

In the IF part, enter RebootDateTime in the right box of the "Output Contains" part.

Row 13e.1

Row 13e.2: Function: Script Log

Row 13e.2

In the script log message, type Reboot successfully scheduled on %output%. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 13e.2 Detail

Row 13e.3: Function: Script Exit

Add a new row by clicking on the Add Row button.

Row 13e.3

In the script exit message, leave it blank.

Row 13e.3 Detail

Row 13f: Function: Script Exit

Row 13f

In the script exit message, type Reboot Schedule Failed with an error %output%. so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device. This will end the task with failure.

Row 13f Detail

Row 14: Complete

Row 14

Row 14 Detail 1

Row 14 Detail 2

Row 14 Detail 3

Deployment

This task can be run manually or in a group. It will only reboot endpoints if the reboot falls under the reboot window provided in the company-level custom fields.

CW RMM - Custom Fields - Reboot Trigger/Schedule

  • Go to Automation > Tasks.
  • Search for Server Reboot Schedule on Specific Days task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task/script.

Schedule Task

This screen will appear.

Schedule Task Screen

  • Select the relevant time to run the script and click the 'Does not repeat' button.

Select Time

Select Time Detail

  • Select the target to schedule the 'Server Reboot Schedule on Specific Days'.

For example:

Select Target

  • Now click the Run button once all customization is set to initiate the task.

Run Task

  • The task will start appearing in the Scheduled Tasks.