Adding a second trigger to the task schedule

You have a powershell script that will create a scheduled task for deployment through Octopus to a Windows 2012 server.

Function Create-ScheduledTask($TaskName,$RunAsUser,$TaskRun,$Schedule,$StartTime,$StartDate,$Arguments,$RunWithElevatedPermissions,$Days,$Password){
    # set up
    $Command = "schtasks.exe /create /ru `"$RunAsUser`" /tn `"$TaskName`" /tr `"'$($TaskRun)' $Arguments`" $cmdSchedule $cmdDays $cmdStartDate $cmdStartTime /F $cmdInterval $cmdDuration $cmdRunLevel $cmdPassword"

    echo $Command
    Invoke-Expression $Command            
 }

Trying to add another trigger as part of the same task name on the command line will not work with schtasks.exe, which apparently contradicts the graphical interface where this can be done.

This is the function that was used to create the event trigger, ideally, to join the same scheduled task.

Function Create-ScheduledTaskEvent($TaskName,$RunAsUser,$TaskRun,$Arguments,$RunWithElevatedPermissions,$Password, $xPath, $channelName){

    $cmdRunLevel = if(-Not $RunWithElevatedPermissions){""}else{"/rl HIGHEST"}
    $cmdPassword = if([string]::IsNullOrEmpty($Password)){""}else{"/rp `"$Password`""}
    $cmdXPath = if([string]::IsNullOrEmpty($xPath)){""}else{"/sc ONEVENT /MO `"$xPath`" "}
    $cmdRunLevel = if(-Not $RunWithElevatedPermissions){""}else{"/rl HIGHEST"}

    $Command = "schtasks.exe /create $cmdRunLevel /ru `"$RunAsUser`" $cmdXPath /tn `"$TaskName`" /tr `"'$($TaskRun)' $Arguments`" /ec `"$channelName`" "

    echo $Command          
    Invoke-Expression $Command            
 }

The problem is that replacement of the switch /createon /changeis completed only when the previous run start / actions scheduled task.

Any idea how this can be done using schtasks.exethe command line to combine the triggers into one.

, , , xml, .

+4
2

, , , - / XML. XML script, script XML schtasks.exe .

schtasks.exe /create /RU "NT AUTHORITY\SYSTEM" /TN TaskName /XML "XMLFolder\TaskName.xml"
0

schtasks.exe . schtasks.exe, XML SU.

Scheduled GUI Tasks

, . PowerShell XML [xml] Select-XML, XML, " " , .

, , PowerShell, Register-ScheduledTask, , . Windows Server 2012 R2 Windows 8.1. TechTarget

$triggers = @()
$triggers += New-ScheduledTaskTrigger -Daily -At 03:00
$triggers += New-ScheduledTaskTrigger -Daily -At 09:00
$triggers += New-ScheduledTaskTrigger -Daily -At 15:00
$triggers += New-ScheduledTaskTrigger -Daily -At 21:00

#..... code truncated to only show trigger portion

$action = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
Register-ScheduledTask -TaskName $taskname -Action $action -Trigger $triggers -RunLevel Highest -Description "Test job"

2012 , PSSession , 8.1

0

Source: https://habr.com/ru/post/1653430/


All Articles