Skip to main content

Force Reboot Server With Reboot Windows Verification

Summary

This script is designed to reboot the endpoints based on the custom fields @ServerRebootWindowStart@, @ServerRebootWindowEnd@, and @ServerRebootWindowDay@. If the reboot time does not fall between the defined windows, the reboot will not trigger.

NOTE: To exclude this client from the solution, please enter 'Exclude' in the @ServerRebootWindowDay@ and @WorkstationRebootWindowDay@ fields. This will cause the script to exit and not take any actions on the target partner.

Sample Run

Sample Run Image 1 Sample Run Image 2

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 Image

  • Name: Force Reboot Server With Reboot Windows Verification
  • Description: This script is designed to reboot the endpoints based on the custom fields @ServerRebootWindowStart@, @ServerRebootWindowEnd@, and @ServerRebootWindowDay@. If the reboot time does not fall between the defined windows, the reboot will not trigger.
  • Category: Maintenance

Create Task Image 2

Script

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

Script Image

Row 1: Function: Script Log

Row 1 Image

Paste the highlighted text: Checking OS

Row 1 Image 2

Row 2: Function: PowerShell Script

Row 2 Image Row 2 Image 2

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 Image

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 Image 2

Row 4: Logic: If/Then

Row 4 Image Row 4 Image 2

Row 4a: Condition: Output Contains

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

Row 4a Image

Row 4b: Function: Script Log

Row 4b Image

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. This will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 4b Image 2

Row 4c: Function: Script Exit

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

Row 4c Image

In the script exit message, leave it blank.

Row 4c Image 2

Row 5: Logic: If/Then

Row 5 Image

Row 5a: Condition: Custom Field Equals

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

Row 5a Image

Row 5b: Function: Script Log

Row 5b Image

In the script log message, type Force reboot is allowed on the endpoint. Proceeding for reboot without windows validation. This will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 5b Image 2

Row 5c: Function: Command Prompt (CMD) Script

Row 5c Image Row 5c Image 2

Run it as 'Continue on Failure' and paste.

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

shutdown -r -f -t 60

Row 5d: Function: Script Exit

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

Row 5d Image

In the script exit message, leave it blank.

Row 5d Image 2

Row 6: Function: Set Pre-defined Variable

Row 6 Image

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

Row 6 Image 2

Row 7: Function: Set Pre-defined Variable

Row 7 Image

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

Row 7 Image 2

Row 8: Function: Set Pre-defined Variable

Row 8 Image

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

Row 8 Image 2

Row 9: Logic: If/Then

Row 9 Image

Row 9a: Condition: Custom Field Equals

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

Row 9a Image

Row 9b: Function: Script Log

Row 9b Image

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

Row 9b Image 2

Row 9c: Function: Script Exit

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

Row 9c Image

In the script exit message, leave it blank.

Row 9c Image 2

Row 10: Function: Script Log

Row 10 Image

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@. This will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 10 Image 2

Row 11: Function: PowerShell Script

Row 11 Image Row 11 Image 2

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 Image

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 Image 2

Row 13: Logic: If/Then

Row 13 Image Row 13 Image 2

Row 13a: Condition: Output Contains

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

Row 13a Image

Row 13b: Function: Script Log

Row 13b Image

In the script log message, type Proceeding for the reboot as the reboot falls under the reboot window provided in the script. This will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Row 13b Image 2

Row 13c: Function: Command Prompt (CMD) Script

Row 13c Image Row 13c Image 2

Run it as 'Continue on Failure' and paste.

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

shutdown -r -f -t 60

Row 13d: Function: Script Exit

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

Row 13d Image

In the script exit message, leave it blank.

Row 13d Image 2

Row 14: Complete

The complete script looks as shown below:

Complete Script Image 1 Complete Script Image 2 Complete Script Image 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.

Reboot Trigger/Schedule

  • Go to Automation > Tasks.
  • Search for the Force Reboot Server With Reboot Windows Verification task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task/script.

Schedule Task Image

This screen will appear.

Schedule Screen Image

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

Select Time Image

Select Target Image

  • Select the target to schedule the 'Force Reboot Server With Reboot Windows Verification'.

For example:

Select Target Example Image

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

Run Button Image

  • The task will start appearing in the Scheduled Tasks.

Output

  • Task & Sequences Log