Advertisement
Lorenzo501

windows_schedule_task.ps1

Dec 6th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [string]$taskName,
  3.     [string]$description,
  4.     [string]$executable
  5. )
  6.  
  7. if (-Not (Get-ScheduledTask | Where {$_.TaskName -eq $taskName}))
  8. {
  9.     Write-Host "Creating scheduled task..."
  10.  
  11.     $task = New-ScheduledTaskAction -Execute $executable -Argument ""
  12.  
  13.     $trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
  14.     $trigger.Repetition = (New-ScheduledTaskTrigger -Once -At "12am" -RepetitionInterval (New-TimeSpan -Hours 4) -RepetitionDuration (New-TimeSpan -Hours 24)).repetition
  15.  
  16.     $reg = Register-ScheduledTask -TaskName $taskName -Description $description -Trigger $trigger -Action $task
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement