Search code examples
octopus-deploy

Octopus Deploy Windows Scheduled Task


This question is for all who are using Octopus Deploy to run scheduled tasks.

https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-create

Has anyone encountered situation where you have to specify "Start in (optional):" parameter in the scheduled task?

I am wondering if this is possible with Octopus Deploy or if there is any work around?


Solution

  • Octopus deploy community steps are just Powershell scripts with variables. You can edit the Powershell to setup anyone variable for "Start in" path and pass that to the scheduled task. I can give you an example of you need one.

    Update

    After accuracy looking at the Posh script for the task, I think a better option would be to add a single parameter for the XML file that defines the task parameters and set that inside your Octopus deployment steps. That will give you the most fixability in case you need to provide any other parameters besides the "Start in" parameter.

    Update 2

    So I wrote a custom Step to do what you wanted, then looked at the community feed, silly me. There is already stemp template to create a scheduled task from XML file. The XML will let you set the working directory. The step template is called "Create Scheduled Tasks From XML" and you can find it at http://library.octopusdeploy.com/step-templates/26c779af-4cce-447e-98bb-4741c25e0b3c/actiontemplate-create-scheduled-tasks-from-xml.

    In addition, here is where I was going with the custom step, it's just Powershell:

    $ErrorActionPreference = "Stop";
    Set-StrictMode -Version "Latest";
    
    function New-ScheduledTask {
        param (
            [Parameter(Mandatory = $true)][hashtable] $octopusParameters
        )
    
        $arguments = @{
            TaskName = $octopusParameters['TaskName']
            User = $octopusParameters['RunAsUser']
        }
    
        if ($octopusParameters['RunAsPassword']) {
            $arguments.Password = $runAsPassword
        }
    
        if ($octopusParameters.ContainsKey('RunWithElevatedPermissions')) {
            if ([boolean]::Parse($octopusParameters['RunWithElevatedPermissions'])) {
                $arguments.RunLevel = 'Highest'
            }
        }
    
        switch ($octopusParameters['Schedule']) {
            'Once' {
                $triggerArguments.Once = $true
                $triggerArguments.At = $runAt
            }
            'Daily' {
                $triggerArguments.Daily = $true
                $triggerArguments.At = $runAt
    
                if ($interval) {
                    $triggerArguments.DaysInterval = $octopusParameters['Interval']
                }
            }
            'Weekly' {
                $triggerArguments.Weekly = $true
                $triggerArguments.At = $runAt
    
                if ($interval) {
                    $triggerArguments.WeeksInterval = $octopusParameters['Interval']
                }
            }
            'Startup' {
                $triggerArguments.AtStartup = $true
            }
            'Logon' {
                $triggerArguments.AtLogOn = $true
            }
        }
    
        $actionArguments = @{
            Execute = $octopusParameters['Executable']
            Argument = $octopusParameters['Arguments']
            WorkingDirectory = $octopusParameters['WorkingDirectory']
        }
        $arguments.Action = New-ScheduledTaskAction @actionArguments
    
        $triggerArguments = @{
            TaskName = $taskName
            User = $runAsUser
        }
        $arguments.Trigger = New-ScheduledTaskTrigger @triggerArguments
    
        Write-Output "Creating Scheduled Task - $taskName"
    
        Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction:SilentlyContinue
        Register-ScheduledTask @arguments | Out-Null
    
        Write-Output "Successfully Created $taskName"
    }
    
    # only execute the step if it's called from octopus deploy,
    # and skip it if we're runnning inside a Pester test
    if (Test-Path -Path "Variable:octopusParameters") {
        New-ScheduledTask $octopusParameters
    }