Skip to main content

Force Reboot Workstation With Reboot Windows Verification

Summary

This script is designed to reboot workstation endpoints based on the custom fields @WorkstationRebootWindowStart@, @WorkstationRebootWindowEnd@, and @WorkstationRebootWindowDay@. If the reboot time does not fall between the defined windows, the reboot will not trigger.

Sample Run

Sample Run 1

Sample Run 2

Dependencies

Custom Fields - Reboot Trigger/Schedule

Implementation

Create Task

Force Reboot Workstation With Reboot Windows Verification: To implement this script, please create a new "Script Editor" style script in the system.

Create Task

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

Task Details

Script

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

Add Rows

Row 1: Function: Script Log

Row 1

Paste the highlighted text: Checking OS

Row 1 Log

Row 2: Function: PowerShell Script

Row 2

Row 2 Log

Paste 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 Log

Row 4: Logic: If/Then

Row 4

Row 4 Logic

Row 4a: Condition: Output Contains

In the IF part, enter Workstation 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 Workstation. Please try the script 'Force Reboot Server 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 Log

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 Exit

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 allowed on the endpoint. Proceeding for reboot without windows 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 Log

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

Row 5c

Row 5c CMD

Run it as 'Continue on Failure' and paste.

Paste the following command 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

In the script exit message, leave it blank.

Row 5d Exit

Row 6: Function: Set Pre-defined Variable

Row 6

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

Row 6 Variable

Row 7: Function: Set Pre-defined Variable

Row 7

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

Row 7 Variable

Row 8: Function: Set Pre-defined Variable

Row 8

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

Row 8 Variable

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 Log

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 Exit

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 Log

Row 11: Function: PowerShell Script

Row 11

Row 11 Log

Paste 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 Log

Row 13: Logic: If/Then

Row 13

Row 13 Logic

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

Row 13b: Function: Script Log

Row 13b

In the script log message, type Proceeding for the reboot as the reboot falls under the reboot window provided in the script. 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 Log

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

Row 13c

Row 13c CMD

Run it as 'Continue on Failure' and paste.

Paste the following command 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

In the script exit message, leave it blank.

Row 13d Exit

Row 14: Complete

The complete script looks as shown below:

Complete Script

Complete Script 2

Complete Script 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 Force Reboot Workstation With Reboot Windows Verification Task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task/script.

Schedule Task

This screen will appear.

Schedule Screen

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

Select Time

Select Time 2

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

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.

Output

  • Task & Sequences Log