Why is my Reboot Policy not deploying or running

15 Views Asked by At

Could someone tell me why this script will not run on Microsoft Endpoint. This script is to reboot all computers on network on Tuesday at 2 pm every week with a warning 5 minutes before the reboot allowing the user to accept or decline the reboot.

# Set the reboot day and time
$rebootDay = "Tuesday"
$rebootTime = "14:00"  # 2:00 PM

# Calculate the time for the warning message
$warningTime = (Get-Date).Date.AddHours(13).AddMinutes(55)

# Create a script block for the task action
$actionScript = {
    # Check if it's the reboot day
    if ((Get-Date).DayOfWeek -eq $rebootDay) {
        # Calculate the current time
        $currentTime = Get-Date -Format "h:mm tt"

        # Check if the current time is after the warning time
        if ($currentTime -ge $warningTime.ToString("h:mm tt")) {
            # Show the warning message and get user response
            $result = [System.Windows.Forms.MessageBox]::Show("Your computer will be rebooted in 5 minutes. Do you want to proceed with the reboot?", "Computer Restart", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Warning)

            if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
                # Wait for 5 minutes before forcing the reboot
                Start-Sleep -Seconds 300

                # Perform the reboot
                Restart-Computer -Force
            } else {
                # User declined reboot
                [System.Windows.Forms.MessageBox]::Show("Computer reboot declined. The system will not be rebooted.", "Reboot Declined", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
            }
        }
    }
}

# Define the trigger for the task (Weekly on the specified day and time)
$trigger = New-ScheduledTaskTrigger -Weekly -At $rebootTime -DaysOfWeek $rebootDay

# Create the task action
$action = New-ScheduledTaskAction -Execute PowerShell.exe -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -Command $actionScript"

# Register the scheduled task
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "RebootTask" -Description "Scheduled reboot task with warning" -RunLevel Highest -Force

0

There are 0 best solutions below