5

Have a powershell script that will create a scheduled task, for deployment, by way of 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            
 }

Attempting to add another trigger as part of the same taskname, on the command line, will not work with schtasks.exe which seemingly contradicts the GUI where it can be done.

This is the function that was used to create the event trigger, ideally, to attach that to 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            
 }

Issue is, replacing the switch /create with /change only ends up clobbering the previous scheduled task's trigger/action.

Any idea how can this be done by way of schtasks.exe on the command-line, to combine the triggers into one.

It could be done by creating a separate task schedule with different taskname, it is not ideal though, nor is exporting out task as xml and then importing back in.

TylerH
  • 20,799
  • 66
  • 75
  • 101
tombags
  • 61
  • 1
  • 5

2 Answers2

3

schtasks.exe won't allow you to do this via basic switches. In order to be able to do this with schtasks.exe you can use an XML import, of an existing task, as mentioned on SU.

Scheduled Tasks GUI

Take an export of an existing task with the trigger set you want that you can import later. PowerShell can also natively understand xml files via the [xml] cast and Select-XML so you are not limited to static XML files but can also make changes on the fly to existing ones if you wanted.

Depending on what system you are working from you can also have access to the PowerShell task manipulation cmdlets like Register-ScheduledTask which does allow multiple tasks to be configured. These can be found on Windows Server 2012 R2 and Windows 8.1. To pull a small snippet from 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”

Since you tagged 2012 you might be able to run those cmdlets in a PSSession from your own system if it is not 8.1

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Would that also work for adding an event as well?, e..g ATaskName would have one action that runs at 1am, it would also have an event that is looking for event id and act accordingly? – tombags Sep 02 '16 at 11:49
  • 2
    Hmmm... that one eludes me. I am researching. New-ScheduledTaskTrigger cover logon, startup and time.... don't see a reference to events though. – Matt Sep 02 '16 at 11:58
  • Did find that using `/sc ONEVENT` clobbers any existing `/sc WEEKLY|DAILY` etc. – tombags Sep 02 '16 at 12:00
  • Why is xml _not ideal_ for you btw? – Matt Sep 02 '16 at 12:01
  • its a custom step in Octopus deploy, and requires to be as generic as possible for other deployments. – tombags Sep 02 '16 at 12:56
1

I have similar task and most elegant solution that I found was to create the schedule task at test environment /local PC using the GUI and export it as XML. Then copy the XML formatted text and embedded it into the script, after execution the script save this string block as XML file locally and run schtasks.exe to create the new task.

schtasks.exe /create /RU "NT AUTHORITY\SYSTEM" /TN TaskName /XML "XMLFolder\TaskName.xml"
autosvet
  • 869
  • 1
  • 9
  • 17
  • Did mention that exporting/importing XML is not ideal fit. Thanks though. – tombags Sep 02 '16 at 11:49
  • Yes, I sow it. But This is the only relatively easy for implementation solution that I found. You can extend it and dynamically change the XML if you need. As @Matt ask why you don't want to use the XML way? – autosvet Sep 02 '16 at 12:14
  • No, MS uses PowerShell now: restart-computer -force – Patrick Burwell Nov 03 '22 at 15:07