Windows Management Tools

2022 - 02 - 16

Windows Task Scheduler (任务计划程序)

Examples:

$action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\MyScript.ps1"'
$trigger = New-ScheduledTaskTrigger -Once -At 3am
$settings = New-ScheduledTaskSettingsSet
$task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'MySchedule' -InputObject $task -User 'username' -Password 'passhere'
ScheduledTask -TaskName 'MySchedule'
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
  -Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-1)) | Export-Csv -Path c:\fso\applog.csv -Force -NoTypeInformation}"'
$trigger = New-ScheduledTaskTrigger -Daily -At 0am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AppLog" -Description "Daily dump of Applog"
$action = New-ScheduledTaskAction -Execute 'C:\FreeFileSync\FreeFileSync.exe' -Argument 'C:\myBackup.ffs_batch'
$trigger = New-ScheduledTaskTrigger -Daily -At 0am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "dailyBackup" -Description "Daily green & big files backup"

Note (by default):

  • will not run if the time has passed;
  • only run when NOT using battery power;
  • only run when a user logged in;
  • etc.

refs: techtarget, devblogs.microsoft.

Comments
Write a Comment