Windows Scheduled Task - Change Path

Octopus.Script exported 2014-12-10 by bobjwalker belongs to ‘Windows’ category.

Changes the execution path of a Windows Scheduled Task for both 2008 and 2012.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Task Name

TaskName

Name of the Windows Scheduled Task.

Task Folder

TaskFolder

Folder path of the command to be executed by the Scheduled Task.

User Name

TaskUserName

User name the task will run under.

Password

TaskPassword

Password for the user the task will run under.

Task Executable

TaskExe

Executable name of the task to be run from the Task Folder.

Script body

Steps based on this template will execute the following PowerShell script.

$taskName   = $OctopusParameters['TaskName']
$taskFolder = $OctopusParameters['TaskFolder']
$taskExe    = $OctopusParameters['TaskExe']
$userName   = $OctopusParameters['TaskUserName']
$password   = $OctopusParameters['TaskPassword']

$taskPath = Join-Path $taskFolder $taskExe
Write-Output "Changing execution path of $taskName to $taskPath"

#Check if 2008 Server
if ((Get-WmiObject Win32_OperatingSystem).Name.Contains("2008"))
{
    $userName = "`"$userName`""
    schtasks /Change /RU $userName /RP $password /TR $taskPath /TN $taskName
}
else
{
    $action = New-ScheduledTaskAction -Execute $taskPath
    Set-ScheduledTask -TaskName $taskName -Action $action -User $userName -Password $password;
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "406ae0ad-72ce-491f-89a3-22bc2dbbb7ed",
  "Name": "Windows Scheduled Task - Change Path",
  "Description": "Changes the execution path of a Windows Scheduled Task for both 2008 and 2012.",
  "Version": 4,
  "ExportedAt": "2014-12-10T18:00:42.218+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "TaskName",
      "Label": "Task Name",
      "HelpText": "Name of the Windows Scheduled Task.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "TaskFolder",
      "Label": "Task Folder",
      "HelpText": "Folder path of the command to be executed by the Scheduled Task.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "TaskUserName",
      "Label": "User Name",
      "HelpText": "User name the task will run under.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "TaskPassword",
      "Label": "Password",
      "HelpText": "Password for the user the task will run under.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "TaskExe",
      "Label": "Task Executable",
      "HelpText": "Executable name of the task to be run from the Task Folder.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$taskName   = $OctopusParameters['TaskName']\n$taskFolder = $OctopusParameters['TaskFolder']\n$taskExe    = $OctopusParameters['TaskExe']\n$userName   = $OctopusParameters['TaskUserName']\n$password   = $OctopusParameters['TaskPassword']\n\n$taskPath = Join-Path $taskFolder $taskExe\nWrite-Output \"Changing execution path of $taskName to $taskPath\"\n\n#Check if 2008 Server\nif ((Get-WmiObject Win32_OperatingSystem).Name.Contains(\"2008\"))\n{\n    $userName = \"`\"$userName`\"\"\n    schtasks /Change /RU $userName /RP $password /TR $taskPath /TN $taskName\n}\nelse\n{\n    $action = New-ScheduledTaskAction -Execute $taskPath\n    Set-ScheduledTask -TaskName $taskName -Action $action -User $userName -Password $password;\n}",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-scheduled-task-changepath.json",
  "Website": "/step-templates/406ae0ad-72ce-491f-89a3-22bc2dbbb7ed",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Wednesday, December 10, 2014