Creating scheduled tasks via PowerShell allows for automated system management, such as scheduling reboots. This script generates a task that reboots the system daily at a random minute past 6 AM and includes an option to disable time zone synchronization.
Creating the Scheduled Task
This script creates a daily scheduled task to reboot the computer.
$minute= Get-Random -Maximum 60
$minute= $minute.ToString()
if ($minute.Length -eq 1) {$minute = "0" + $minute}
$HeureReboot = "06:$minute"
Write-Host "Scheduled reboot time: $HeureReboot"
$User = "NT AUTHORITY\SYSTEM"
$Trigger = New-ScheduledTaskTrigger -Daily -At $HeureReboot
$Action = New-ScheduledTaskAction -Execute "c:\windows\system32\shutdown.exe" -Argument "-r -f -t 00"
Register-ScheduledTask -TaskName "Reboot" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
Alternative to Disable Time Zone Synchronization
This alternative script disables the “Synchronize across time zones” option for the scheduled task.
$minute = Get-Random -Maximum 60
$minute = $minute.ToString()
if ($minute.Length -eq 1) {$minute = "0" + $minute}
Write-Host "Scheduled reboot minute: $minute"
$User = "NT AUTHORITY\SYSTEM"
$Date = Get-Date -Date "2020-02-01 06:$minute"
$Trigger = New-ScheduledTaskTrigger -Daily -At (Get-Date -Day 1 -Month 1 -Year 1 -Hour 0)
$Trigger.StartBoundary = (Get-Date -Date $Date -Format 'yyyy-MM-ddTHH:mm:ss')
$Action = New-ScheduledTaskAction -Execute "c:\windows\system32\shutdown.exe" -Argument "-r -f -t 00"
Register-ScheduledTask -TaskName "Reboot" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
0 Comments