3

I'm trying to create a scheduled task with powershell, the task is supposed to run the 1st of every month, but I can't figure out how to use New-ScheduledTaskTrigger with a monthly interval.
EG:

$jobName = "Backup_EMR_Data"  
$action = New-ScheduledTaskAction -Execute $actionName -Argument  $arg -WorkingDirectory $SSISPackagePath  
$trigger = New-ScheduledTaskTrigger -Daily -At 12:30AM   
$settings = New-ScheduledTaskSettingsSet  
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings  

Register-ScheduledTask $jobName -InputObject $task -TaskPath $taskPath -User $userName -Password $password
0

I can't figure out how to use New-ScheduledTaskTrigger with a monthly interval.

$triggers = $TaskDefinition.Triggers
$trigger = $triggers.Create(1) # Creates a "One time" trigger

Now if we wanted to run the Task more than just once, let’s say on a monthly basis, we have to change and add a bit of code The code above uses Create(1) which means that the trigger is set to run once.

$trigger = $triggers.Create(1) # Creates a "One time" trigger

If we want to use another schedule we must use one of the following values as explained in more detail here.

TASK_TRIGGER_EVENT                   0
TASK_TRIGGER_TIME                    1
TASK_TRIGGER_DAILY                   2
TASK_TRIGGER_WEEKLY                  3
TASK_TRIGGER_MONTHLY                 4
TASK_TRIGGER_MONTHLYDOW              5
TASK_TRIGGER_IDLE                    6
TASK_TRIGGER_REGISTRATION            7
TASK_TRIGGER_BOOT                    8
TASK_TRIGGER_LOGON                   9
TASK_TRIGGER_SESSION_STATE_CHANGE   11

So let’s suppose we want to run the task the first day of every month, we then have to change the code as following.

$trigger = $triggers.Create(4)
$trigger.DaysOfMonth = 1

Source PowerShell – Creating Scheduled Tasks with PowerShell version 3

  • Does this still work with PSv4, which is what the questioner seems to be using? According to this it can't be done directly, and an XML solution is proposed. – AFH Nov 4 '15 at 12:32
  • @AFH I don't know. Lets wait for the OP to respond. – DavidPostill Nov 4 '15 at 12:35
  • PSv3 I don't have any Scheduled Task cmdlets. Schedule Job cmdlets use triggers, and I found the enum values usable for those triggers: PS > [enum]::GetValues([Microsoft.PowerShell.ScheduledJob.TriggerFrequency]) results in None, Once, Daily, Weekly, AtLogon, AtStartup. This does not necessarily mean Monthly is not available to his version/module, but a similar exercise would answer the question. – Xalorous Nov 5 '15 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.