简体   繁体   中英

Create Task Scheduler with monthly execution using powershell

I am trying to create a task scheduler like below

在此处输入图像描述

So far I came up with below code.

#Variables
$TaskName = "TestingTask"
$username ="user"
$password ='password'
$description = "This is a testing task"
 
#Create Scheduled Task
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "E:\script.ps1"
$Trigger = New-ScheduledTaskTrigger -At 10AM -Weekly -WeeksInterval 2 -DaysOfWeek Friday
$taskPrincipal = New-ScheduledTaskPrincipal -UserId 'user' -RunLevel Highest
$ScheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Description $description -Principal $taskPrincipal
 
Register-ScheduledTask -TaskName $TaskName -InputObject $ScheduledTask -User $username -Password $password

Please let me know how can we schedule monthly like this.

From the help for the cmdlet Get-Help New-ScheduledTaskTrigger -full

Example 4: Register a scheduled task that starts every-other week

PS C:\>$Sta = New-ScheduledTaskAction -Execute "Cmd"

The second command creates a scheduled task trigger that starts every other Sunday at 3:00 A.M and assigns the ScheduledTaskTrigger object to the $Stt variable.
PS C:\>$Stt = New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Sunday -At 3am

The third command registers the scheduled task Task01 to run the task action named Cmd every other Sunday at 3:00 A.M.
PS C:\>Register-ScheduledTask Task01 -Action $Sta -Settings $Stt

This example registers a scheduled task that starts every other week.

The first command creates a scheduled task action named Cmd and assigns the ScheduledTaskAction object to the $Sta variable.

I would assume that if you set -Weekly -WeeksInterval 4 should set the task to run once every 4 weeks, and should essentially make it a month. But you are right, there is no real option to identify a "month", you should try this out and see how it looks like the GUI.

Another thing to try is, to make a montly based task in the GUI and use Get-ScheduledTask to see how the interval is configured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM