Search code examples
windowspowershellwindows-task-scheduler

Having trouble getting this scheduled task to run


I have this script that created the scheduled task to reboot machines on a weekly basis. The task is successfully created but won't automatically run at designated time.

# Create task action
$taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer - Force'
# Create a trigger (Thursday at 9:00 AM)
$taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Thursday -At 9:00am
# The user to run the task
$taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
# The name of the scheduled task.
$taskName = "Weekly Reboot"
# Describe the scheduled task.
$description = "Forcibly reboot the computer at 9:00am on Thursday"
# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal     $taskUser -Description $description
$shellObject = New-Object -ComObject Wscript.Shell
$notification = $shellObject.Popup("Computer will reboot in 30 minutes",0,"Reboot Notification")`

Task deployed successfully and can be seen in Task Scheduler but won't run automatically.


Solution

  • Try it with another user:

    -UserId "NT AUTHORITY\SYSTEM"