40346

Powershell Scheduled Task At Startup Repeating

<h3>Question</h3>

I'm trying to create a Scheduled Task with the following Trigger:
- Startup
- Run every 5 minutes
- Run indefinitely

In the GUI I can do this easily by selecting:
- Begin the task: at startup
And in the Advanced tab:
- Repeat task every: 5 minutes
- For a duration of: indefinitely

But I'm having trouble doing it with Powershell.

My troubled code:
$repeat = (New-TimeSpan -Minutes 5)
$duration = ([timeSpan]::maxvalue)
$trigger = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval $repeat -RepetitionDuration $duration

It won't take the RepetitionInterval and RepetitionDuration parameters. But I need that functionality. How could I accomplish my goal?


<h3>Answer1:</h3>

To set the task literally with a "Startup" trigger and a repetition, it seems you have to reach in to COM (or use the TaskScheduler UI, obviously..).

# Create the task as normal $action = New-ScheduledTaskAction -Execute "myApp.exe" Register-ScheduledTask -Action $action -TaskName "My Task" -Description "Data import Task" -User $username -Password $password # Now add a special trigger to it with COM API. # Get the service and task $ts = New-Object -ComObject Schedule.Service $ts.Connect() $task = $ts.GetFolder("\").GetTask("My Task").Definition # Create the trigger $TRIGGER_TYPE_STARTUP=8 $startTrigger=$task.Triggers.Create($TRIGGER_TYPE_STARTUP) $startTrigger.Enabled=$true $startTrigger.Repetition.Interval="PT10M" # ten minutes $startTrigger.Repetition.StopAtDurationEnd=$false # on to infinity $startTrigger.Id="StartupTrigger" # Re-save the task in place. $TASK_CREATE_OR_UPDATE=6 $TASK_LOGIN_PASSWORD=1 $ts.GetFolder("\").RegisterTaskDefinition("My Task", $task, $TASK_CREATE_OR_UPDATE, $username, $password, $TASK_LOGIN_PASSWORD)
<h3>Answer2:</h3>

New-ScheduledTaskTrigger uses parameter sets. When you specify that you want the scheduled task to start up "at logon" you are restricting yourself to the following parameter set:

Parameter Set: AtStartup New-ScheduledTaskTrigger [-AtStartup] [-RandomDelay <TimeSpan> ] [ <CommonParameters>]

What may be more beneficial is if you use your "at startup" scheduled task to register a new scheduled task to run every five minutes using the "once" parameter set:

Parameter Set: Once New-ScheduledTaskTrigger [-Once] -At <DateTime> [-RandomDelay <TimeSpan> ] [-RepetitionDuration <TimeSpan> ] [-RepetitionInterval <TimeSpan> ] [ <CommonParameters>]

Your Scheduled Task Trigger should successfully be assigned once you are using the correct parameter set.


<h3>Answer3:</h3>

See my answer here: https://stackoverflow.com/a/61646465/2943191

It is actually relatively simple, once a working example is written, of course.

Instead of -AtLogOn use -AtStartup.

来源:https://stackoverflow.com/questions/47611878/powershell-scheduled-task-at-startup-repeating

Recommend

  • Powershell Scheduled Task At Startup Repeating
  • HTML/PHP/JS Select multiple file and upload via FTP
  • Forming a matrix by summing 2 arrays element-array wise using NumPy
  • Is there a numpy function to get the sum of sub matrices?
  • Auto change order status from hold-on to processing in Woocommerce
  • jasperreports subreport not splitting
  • How come my event.currentTarget is changing automatically?
  • Qt: How to set header on child tables in QAbstractItemModel?
  • Descending order using heapq
  • Haskell Random Generator … how to make easier to use?
  • How to remove space/TAB from command output
  • Matlab structure in Python [duplicate]
  • Ruby: Write to CSV if condition met
  • Aggregate 1D arrays of various lengths into 2D array
  • Duplicated entries to mySQL using jQuery ajax and PHP
  • JQuery Cycle and JSON with JQuery
  • How to install or uninstall SonarQube plug-ins with HTTP?
  • bootstrappedUser - Jade or EJS to HTML
  • React / Webpack - “Module parse failed: Unexpected token - You may need an appropriate loader to han
  • Why am I getting an Argument exception when creating event handler dynamically?
  • PowerShell script to pass SecureString to Plink as account and sudo passwords
  • Write to elasticsearch from spark is very slow
  • How to find angle formed by the blades of a wind turbine with respect to a horizontal imaginary axis
  • How to use Flask's render_template from an ajax POST form submit
  • Swift: UIView.animate works unexpectedly
  • Silverlight Event Log in Isolated Storage
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Cloud Code: Creating a Parse.File from URL
  • How to call jQuery function in HTML returned by AJAX
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to warp text around image in iOS?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?