Job Trigger cmdlets - PowerShell
Overview
The Job Trigger cmdlets in PowerShell manage triggers for scheduled jobs. Triggers define when a scheduled job will run, allowing for flexible automation based on specific conditions.
Syntax
Get-JobTrigger
Get-JobTrigger [-JobName] <string> [-TriggerName] <string> [-Filter] <string> [-ErrorAction] <ActionPreference> [-WarningAction] <ActionPreference> [-OutBuffer] <bool> [-OutVariable] <string> [CommonParameters]
New-JobTrigger
New-JobTrigger [-JobName] <string> [-Name] <string> [-TriggerType] <string> [-Schedule] <string> [-RepetitionInterval] <string> [-RetryInterval] <string> [-MaxRetryAttempts] <int> [-Enabled] <bool> [CommonParameters]
Remove-JobTrigger
Remove-JobTrigger [-JobName] <string> [-TriggerName] <string> [-Confirm] <bool> [-WhatIf] <bool> [CommonParameters]
Set-JobTrigger
Set-JobTrigger [-JobName] <string> [-TriggerName] <string> [-Schedule] <string> [-RepetitionInterval] <string> [-RetryInterval] <string> [-MaxRetryAttempts] <int> [-Enabled] <bool> [CommonParameters]
Options/Flags
Get-JobTrigger
- -JobName: Specifies the name of the job whose triggers are to be retrieved.
- -TriggerName: Filters the results to a specific trigger by name.
- -Filter: Specifies a filter expression to narrow down the results.
New-JobTrigger
- -JobName: Specifies the name of the job to which the trigger will be added.
- -Name: Sets the name of the new trigger.
- -TriggerType: Indicates the type of trigger to create: “Daily”, “Weekly”, “Monthly”, “Once”, “AtLogOn”, or “AtStartup”.
- -Schedule: Defines a cron-like schedule for the trigger.
- -RepetitionInterval: Sets the interval at which the trigger repeats.
- -RetryInterval: Specifies the time to wait between trigger retries.
- -MaxRetryAttempts: Defines the maximum number of times a trigger should be retried.
- -Enabled: Determines whether the trigger is enabled or not.
Remove-JobTrigger
- -Confirm: Prompts for confirmation before removing the trigger.
- -WhatIf: Simulates the removal operation without actually performing it.
Set-JobTrigger
- -JobName: Specifies the name of the job whose trigger is being modified.
- -TriggerName: Identifies the trigger to be modified.
- -Schedule: Updates the schedule for the trigger.
- -RepetitionInterval: Modifies the repetition interval.
- -RetryInterval: Changes the retry interval.
- -MaxRetryAttempts: Adjusts the maximum number of retry attempts.
- -Enabled: Enables or disables the trigger.
Examples
Get all triggers for a job
Get-JobTrigger -JobName MyJob
Create a daily trigger
New-JobTrigger -JobName MyJob -Name DailyTrigger -TriggerType Daily -Schedule "0 0 * * *"
Disable a trigger
Set-JobTrigger -JobName MyJob -TriggerName MonthlyTrigger -Enabled $false
Delete a trigger
Remove-JobTrigger -JobName MyJob -TriggerName AtLogOn
Common Issues
- Ensure that the specified job exists before creating or modifying triggers.
- Avoid using overly complex schedules that may lead to unintended execution times.
- If triggers are not executing as expected, check the event logs for any errors or warnings.
Integration
Job triggers can be combined with other PowerShell commands to create sophisticated automation tasks. For example, you can use Get-JobTrigger to retrieve trigger information and then Set-JobTrigger to modify the schedule based on external events.
Related Commands
- Get-Job
- Start-Job
- Stop-Job