How to Get Friendly Description For Scheduled Task Trigger in Powershell - powershell

Following https://stackoverflow.com/a/32732879/1378356, I can get a scheduled task's trigger using this syntax:
$task = Get-ScheduledTask -TaskName "SomeTaskName"
$taskTrigger = $task.Triggers[0]
$taskTrigger
But how do I see the friendly name (which is displayed in the Task Scheduler UI), such as "At 8:00 am every Monday"?

Related

Change a property of a task in task scheduler using powershell

I have two triggers in a task. The first one runs at a specific time. The second one starts at logon and runs every 10 minutes. I have many similar tasks like this situation. I want to use powershell to change the property from 10 minutes to 5 minutes and run indefinitely after logon. How do I specify the SECOND trigger?
$Task = Get-ScheduledTask -TaskName "Task"
$Task.Triggers.LogonTriggers.Repetition.Duration = ""
$Task.Triggers.Repetition.Interval = "PT10M"
You can modify the $Task object and pass it into the Set-ScheduledTask which will apply the changes you've made. The first trigger that runs at a specific time will have it's StartBoundary property set, the second trigger which starts at Logon won't have this property set so we'll use the value of that to make sure we change the correct trigger.
$Task = Get-ScheduledTask -TaskName "Task"
$RepeatingTrigger = $Task.Triggers | Where-Object { $_.StartBoundary -eq $null }
$RepeatingTrigger.Repetition.Interval = "PT5M"
Set-ScheduledTask -InputObject $Task

How to configure "If the task is already running, then the following rule applies" in Windows Task Scheduler using PowerShell script?

I am trying to achieve the following settings (select "If the task is already running, then the following rule applies") through PowerShell script but unable to get appropriate settings to configure that.
I am using the following code to configure that
$Trigger = New-ScheduledTaskTrigger -At 07:00am -Daily
$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hour 1) -Compatibility Win7 -StartWhenAvailable -Priority 7
$User = "SYSTEM"
$Action = New-ScheduledTaskAction -Execute "some script" -Argument "some argument" -WorkingDirectory "working dir"
Register-ScheduledTask -TaskName "Test Task" -Trigger $Trigger -User $User -Action $Action -Settings $Settings -RunLevel Highest –Force
To do the advanced configuration for the triggers
$Task = Get-ScheduledTask -TaskName "Example Task"
$Task.Triggers[0].ExecutionTimeLimit = "PT10M"
$Task | Set-ScheduledTask -User $User
The setting is configured via New-ScheduledTaskSettingsSet and the parameter you're looking for is -MultipleInstances:
-MultipleInstances
Specifies the policy that defines how Task Scheduler handles multiple instances of the task. The acceptable values for this parameter are:
IgnoreNew. The new task instance is ignored. Parallel. The new task instance starts immediately. Queue. The new task instance starts as soon as the current instance completes.
Type: MultipleInstancesEnum
Accepted values: Parallel, Queue, IgnoreNew
Position: Named
Default value: None
However, the documentation lists only 3 values, and the respective enum (at least at the time of this writing also only has the listed 3 values:
Parallel → Run a new instance in parallel
Queue → Queue a new instance
IgnoreNew → Do not start a new instance
If you create a task manually via the GUI and select the setting "Stop the existing instance" the value .Settings.MultipleInstances is empty, but if you create a Settings object via New-ScheduledTaskSettingsSet omitting the parameter -MultipleInstances it defaults to IgnoreNew. Attempts to change that to an empty value result in validation errors.
This is obviously a bug (missing value in the referenced enum).
The enum now contains 'StopExisting'.
This is my solution in C#.
static void RegisterMyTask(string taskPath, string remoteServer)
{
try
{
using TaskService ts = new(remoteServer);
TaskDefinition taskDef = ts.NewTask();
taskDef.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;
...
However PowerShell probably has the new enum as well. As I would guess:
-MultipleInstances StopExisting
I had a task scheduled on multiple remote servers.
On day 1 it started running.
Day 2 it failed to start a new instance, but didn't kill the existing instance
Day 3 it failed to start a new instance, but didn't kill the existing instance. Then 30 seconds later, the existing instance was killed by the day 1 timeout setting, but the new instance had already failed for the day.
This fixed my problem.

How to edit a scheduled task trigger

$taskExists = Get-ScheduledTask $strTaskName
if($taskExists) {
$taskExists.Description = "a daily Report"
$taskExists | set-scheduledtask -user $strUser -Password $strPassword
}
The above piece of power shell code is to update existing schedule task. Could anyone please tell me how to update the trigger values?
The task returned from the Get-ScheduledTask cmdlet has a Triggers property where you can add / remove triggers.

Update a Scheduled Task Action using Powershell 4.0

I have already searched this site and also done a good amount of googling and I am unable to find a why to do this.
I want to be able to update the action/s on a scheduled task using PS 4.0.
I don't mind if it means an action to delete the current Action and adding a new one.
Does anyone know if this is possible?
You can use the following to replace the current action with a new action object in a scheduled task.
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe"
Set-ScheduledTask -TaskName "YourTaskName" -Action $Action
This will set a new action to run powershell.exe in a scheduled task.

Enable task to be deleted after X time

How can I enable a task created using PowerShell's Register-ScheduledTask to be deleted If the task is not scheduled to run again? As in New-JobTrigger -Once -At $ScheduledTime
The option is seen in the Task Scheduler GUI > Task Properties > Settings tab > The last checkbox option reads:
If the task is not scheduled to run again, delete it after: <time period>
MS TechNet article Searching for enabling this option using PowerShell does not turn up any relevant results, mostly how to enable tasks and so on.
You should use a New-ScheduledTaskSettingsSet like
New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter <TimeSpan>
$STSet = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter <TimeSpan>
Register-ScheduledTask mytask -Action <actionobject> -Settings $STSet