Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
Created October 14, 2017 19:10
Show Gist options
  • Select an option

  • Save gregjhogan/9c472f495be2d8316e3bc5c6bbdab33c to your computer and use it in GitHub Desktop.

Select an option

Save gregjhogan/9c472f495be2d8316e3bc5c6bbdab33c to your computer and use it in GitHub Desktop.
Windows scheduled task that stays running forever and restarts if it dies
$folder = "C:\folder"
$cmd = "powershell.exe "
$args = "-NoLogo -NonInteractive -WindowStyle Maximized -File script.ps1"
$user = "username-to-run-as"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval (New-TimeSpan -Minutes 1)
$action = New-ScheduledTaskAction -WorkingDirectory $folder -Execute $cmd -Argument $args
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 999 -ExecutionTimeLimit (New-TimeSpan -Seconds 0)
$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive
$task = New-ScheduledTask -Principal $principal -Action $action -Settings $settings -Trigger $trigger
Register-ScheduledTask -TaskName Selenium -InputObject $task -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment