Octopus.Script exported 2017-03-13 by bobjwalker belongs to ‘Windows’ category.
Disables 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.
Maximum Wait Time
MaximumWaitTime = 0
Maximum time the script must wait before aborting. Use ‘0’ to wait indefinitely.
Succeed On Task Not Found
SucceedOnTaskNotFound =
This setting prevents the script from throwing an error if the task is not found.
Script body
Steps based on this template will execute the following PowerShell script.
$taskName = $OctopusParameters['TaskName']
$maximumWaitTime = $OctopusParameters['MaximumWaitTime']
$succeedOnTaskNotFound = $OctopusParameters['SucceedOnTaskNotFound']
#Check if the PowerShell cmdlets are available
$cmdletSupported = [bool](Get-Command -Name Get-ScheduledTask -ErrorAction SilentlyContinue)
try {
if($cmdletSupported) {
$taskExists = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName }
}
else {
$taskService = New-Object -ComObject "Schedule.Service"
$taskService.Connect()
$taskFolder = $taskService.GetFolder('\')
$taskExists = $taskFolder.GetTasks(0) | Select-Object Name, State | Where-Object { $_.Name -eq $taskName }
}
if(-not $taskExists) {
if( $succeedOnTaskNotFound){
Write-Output "Scheduled task '$taskName' does not exist"
}
else {
throw "Scheduled task '$taskName' does not exist"
}
return
}
Write-Output "Disabling $taskName..."
$waited = 0
if($cmdletSupported) {
$task = Disable-ScheduledTask $taskName
Write-Output "Waiting until $taskName is disabled..."
while(($task.State -ne [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]::Disabled) -and (($maximumWaitTime -eq 0) -or ($waited -lt $maximumWaitTime)))
{
Start-Sleep -Milliseconds 200
$waited += 200
$task = Get-ScheduledTask $taskName
}
if($task.State -ne [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]::Disabled) {
throw "The scheduled task $taskName could not be disabled within the specified wait time"
}
}
else {
schtasks /Change /Disable /TN "$taskName"
#The State property can hold the following values:
# 0: Unknown
# 1: Disabled
# 2: Queued
# 3: Ready
# 4: Running
while(($taskFolder.GetTask($taskName).State -ne 1) -and (($maximumWaitTime -eq 0) -or ($waited -lt $maximumWaitTime))) {
Start-Sleep -Milliseconds 200
$waited += 200
}
if($taskFolder.GetTask($taskName).State -ne 1) {
throw "The scheduled task '$taskName' could not be disabled within the specified wait time"
}
}
}
finally {
if($taskFolder -ne $NULL) {
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($taskFolder)
}
if($taskService -ne $NULL) {
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($taskService)
}
}
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "1be30b21-ba58-4667-bff4-2d0ef9a806af",
"Name": "Windows Scheduled Task - Disable",
"Description": "Disables a Windows Scheduled Task for both 2008 and 2012.",
"Version": 6,
"ExportedAt": "2017-03-13T19:50:10.728Z",
"ActionType": "Octopus.Script",
"Author": "bobjwalker",
"Parameters": [
{
"Name": "TaskName",
"Label": "Task Name",
"HelpText": "Name of the Windows Scheduled Task.",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"DefaultValue": null
},
{
"Name": "MaximumWaitTime",
"Label": "Maximum Wait Time",
"HelpText": "Maximum time the script must wait before aborting. Use '0' to wait indefinitely.",
"DefaultValue": 0,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "SucceedOnTaskNotFound",
"Label": "Succeed On Task Not Found",
"HelpText": "This setting prevents the script from throwing an error if the task is not found.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
},
"Links": {}
}
],
"Properties": {
"Octopus.Action.Script.ScriptBody": "$taskName = $OctopusParameters['TaskName']\n$maximumWaitTime = $OctopusParameters['MaximumWaitTime']\n$succeedOnTaskNotFound = $OctopusParameters['SucceedOnTaskNotFound']\n\n#Check if the PowerShell cmdlets are available\n$cmdletSupported = [bool](Get-Command -Name Get-ScheduledTask -ErrorAction SilentlyContinue)\n\ntry {\n\tif($cmdletSupported) {\n\t\t$taskExists = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName }\n\t}\n\telse {\n\t\t$taskService = New-Object -ComObject \"Schedule.Service\"\n\t\t$taskService.Connect()\n\t\t$taskFolder = $taskService.GetFolder('\\')\n\t\t$taskExists = $taskFolder.GetTasks(0) | Select-Object Name, State | Where-Object { $_.Name -eq $taskName }\n\t}\n\n\tif(-not $taskExists) {\n if( $succeedOnTaskNotFound){\n Write-Output \"Scheduled task '$taskName' does not exist\"\n }\n else {\n\t\t throw \"Scheduled task '$taskName' does not exist\"\n }\n\t\treturn\n\t}\n\n\tWrite-Output \"Disabling $taskName...\"\n\t$waited = 0\n\tif($cmdletSupported) {\n\t\t$task = Disable-ScheduledTask $taskName\n\t\tWrite-Output \"Waiting until $taskName is disabled...\"\n\t\twhile(($task.State -ne [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]::Disabled) -and (($maximumWaitTime -eq 0) -or ($waited -lt $maximumWaitTime))) \n\t\t{\n\t\t\tStart-Sleep -Milliseconds 200\n\t\t\t$waited += 200\n\t\t\t$task = Get-ScheduledTask $taskName\n\t\t}\n\t\t\n\t\tif($task.State -ne [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]::Disabled) {\n\t\t\tthrow \"The scheduled task $taskName could not be disabled within the specified wait time\"\n\t\t}\n\t}\n\telse {\n\t\tschtasks /Change /Disable /TN \"$taskName\"\n\t\t#The State property can hold the following values:\n\t\t# 0: Unknown\n\t\t# 1: Disabled\n\t\t# 2: Queued\n\t\t# 3: Ready\n\t\t# 4: Running\n\t\twhile(($taskFolder.GetTask($taskName).State -ne 1) -and (($maximumWaitTime -eq 0) -or ($waited -lt $maximumWaitTime))) {\n\t\t\tStart-Sleep -Milliseconds 200\n\t\t\t$waited += 200\n\t\t}\n\t\t\n\t\tif($taskFolder.GetTask($taskName).State -ne 1) {\n\t\t throw \"The scheduled task '$taskName' could not be disabled within the specified wait time\"\n\t\t}\n\t}\n}\nfinally {\n if($taskFolder -ne $NULL) {\n\t [System.Runtime.Interopservices.Marshal]::ReleaseComObject($taskFolder) \n\t}\n\t\n\tif($taskService -ne $NULL) {\n\t [System.Runtime.Interopservices.Marshal]::ReleaseComObject($taskService) \n\t}\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-disable.json",
"Website": "/step-templates/1be30b21-ba58-4667-bff4-2d0ef9a806af",
"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"
}
}
Page updated on Monday, March 13, 2017