Windows Service - Set Recovery on Failure Actions

Octopus.Script exported 2017-04-11 by samcook belongs to ‘Windows’ category.

Set the recovery on failure actions for a particular service.

Parameters

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

Service Name

ServiceName =

The name of the service you wish to set the recovery options on.

First Failure Action

FirstFailureAction = restart

The action you wish the service to take after this failure. Defaults to ‘Restart Service’.

First Failure Delay

FirstFailureDelay = 180000

The time in milliseconds to wait before preforming the failure action. Defaults to 180000 milliseconds.

Second Failure Action

SecondFailureAction = restart

The action you wish the service to take after this failure. Defaults to ‘Restart Service’.

Second Failure Delay

SecondFailureDelay = 180000

The time in milliseconds to wait before preforming the failure action. Defaults to 180000 milliseconds.

Third Failure Action

ThirdFailureAction = restart

The action you wish the service to take after this failure. Defaults to ‘Restart Service’.

Third Failure Delay

ThirdFailureDelay = 180000

The time in milliseconds to wait before preforming the failure action. Defaults to 180000 milliseconds.

Reset

Reset = 86400

Specifies the length of time (in seconds) with no failures after which the failure count should be reset to 0. Defaults to 86400 seconds.

Script body

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

cls
#ignore above

function main
{
    $serviceName = Get-OctoParameter -parameterName "ServiceName" -parameterDescription "Service Name"
    $firstFailureAction = Get-OctoParameter -parameterName "FirstFailureAction" -parameterDescription "First Failure Action" -default "restart"
    $secondFailureAction = Get-OctoParameter -parameterName "SecondFailureAction" -parameterDescription "Second Failure Action" -default "restart"
    $thirdFailureAction = Get-OctoParameter -parameterName "ThirdFailureAction" -parameterDescription "Third Failure Action" -default "restart"
    $firstFailureDelay = Get-OctoParameter -parameterName "FirstFailureDelay" -parameterDescription "First Failure Delay" -default 180000
    $secondFailureDelay = Get-OctoParameter -parameterName "SecondFailureDelay" -parameterDescription "Second Failure Delay" -default 180000
    $thirdFailureDelay = Get-OctoParameter -parameterName "ThirdFailureDelay" -parameterDescription "Third Failure Delay" -default 180000
    $reset = Get-OctoParameter -parameterName "Reset" -parameterDescription "Reset" -default 86400

    $service = Get-Service $serviceName -ErrorAction SilentlyContinue

    if (!$service)
    {
        Write-Host "Windows Service '$serviceName' not found, skipping."
        return
    }

    echo "Updating the '$serviceName' service with recovery options..."
    echo "    On first failure '$firstFailureAction' after '$firstFailureDelay' milliseconds."
    echo "    On second failure '$secondFailureAction' after '$secondFailureDelay' milliseconds."
    echo "    On third failure '$thirdFailureAction' after '$thirdFailureDelay' milliseconds."
    echo "    Reset after '$reset' minutes."

    sc.exe failure $service.Name actions= $firstFailureAction/$firstFailureDelay/$secondFailureAction/$secondFailureDelay/$thirdFailureAction/$thirdFailureDelay reset= $reset

    echo "Done"
}

function Get-OctoParameter() 
{
    Param
    (
        [Parameter(Mandatory=$true)]$parameterName,
        [Parameter(Mandatory=$true)]$parameterDescription,
        [Parameter(Mandatory=$false)]$default
    )

    $ErrorActionPreference = "SilentlyContinue" 
    $value = $OctopusParameters[$parameterName] 
    $ErrorActionPreference = "Stop" 
    
    if (! $value) 
    {
        if(! $default) 
        {
            throw "'$parameterDescription' cannot be empty, please specify a value."
        }

        return $default
    }
    
    return $value
}

main

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": "5576faaf-e024-4248-ad98-41717a7c4f43",
  "Name": "Windows Service - Set Recovery on Failure Actions",
  "Description": "Set the recovery on failure actions for a particular service.",
  "Version": 12,
  "ExportedAt": "2017-04-11T16:38:17.527Z",
  "ActionType": "Octopus.Script",
  "Author": "samcook",
  "Parameters": [
    {
      "Id": "a4df7a56-844b-4a5b-b233-3aaf5014808e",
      "Name": "ServiceName",
      "Type": "String",
      "Label": "Service Name",
      "HelpText": "The name of the service you wish to set the recovery options on.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "600a43c3-e0b3-4fcd-9832-22ebfef110e9",
      "Name": "FirstFailureAction",
      "Type": "String",
      "Label": "First Failure Action",
      "HelpText": "The action you wish the service to take after this failure. _Defaults to '**Restart Service**'._",
      "DefaultValue": "restart",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "\"\"|Take No Action\nrestart|Restart Service\nreboot|Reboot Computer"
      },
      "Links": {}
    },
    {
      "Id": "a8086832-de84-4ab0-9dd7-2adc3d6a3478",
      "Name": "FirstFailureDelay",
      "Type": "String",
      "Label": "First Failure Delay",
      "HelpText": "The time in milliseconds to wait before preforming the failure action. _Defaults to **180000** milliseconds_.",
      "DefaultValue": "180000",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "b914c20c-5bb7-4a04-9be5-2090e71be6d7",
      "Name": "SecondFailureAction",
      "Type": "String",
      "Label": "Second Failure Action",
      "HelpText": "The action you wish the service to take after this failure. _Defaults to '**Restart Service**'._",
      "DefaultValue": "restart",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "\"\"|Take No Action\nrestart|Restart Service\nreboot|Reboot Computer"
      },
      "Links": {}
    },
    {
      "Id": "c24bed04-1f1f-418c-a60e-eba717d0ed2f",
      "Name": "SecondFailureDelay",
      "Type": "String",
      "Label": "Second Failure Delay",
      "HelpText": "The time in milliseconds to wait before preforming the failure action. _Defaults to **180000** milliseconds_.",
      "DefaultValue": "180000",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "14654da5-dae8-4163-b404-a2f29ea2c518",
      "Name": "ThirdFailureAction",
      "Type": "String",
      "Label": "Third Failure Action",
      "HelpText": "The action you wish the service to take after this failure. _Defaults to '**Restart Service**'._",
      "DefaultValue": "restart",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "\"\"|Take No Action\nrestart|Restart Service\nreboot|Reboot Computer"
      },
      "Links": {}
    },
    {
      "Id": "957f3937-fba6-4ace-ba79-01441c113173",
      "Name": "ThirdFailureDelay",
      "Type": "String",
      "Label": "Third Failure Delay",
      "HelpText": "The time in milliseconds to wait before preforming the failure action. _Defaults to **180000** milliseconds_.",
      "DefaultValue": "180000",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "94d29d24-fb84-4621-acda-d7105ea975b6",
      "Name": "Reset",
      "Type": "String",
      "Label": "Reset",
      "HelpText": "Specifies the length of time (in seconds) with no failures after which the failure count should be reset to 0.  _Defaults to **86400** seconds_.",
      "DefaultValue": "86400",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.ScriptBody": "cls\n#ignore above\n\nfunction main\n{\n    $serviceName = Get-OctoParameter -parameterName \"ServiceName\" -parameterDescription \"Service Name\"\n    $firstFailureAction = Get-OctoParameter -parameterName \"FirstFailureAction\" -parameterDescription \"First Failure Action\" -default \"restart\"\n    $secondFailureAction = Get-OctoParameter -parameterName \"SecondFailureAction\" -parameterDescription \"Second Failure Action\" -default \"restart\"\n    $thirdFailureAction = Get-OctoParameter -parameterName \"ThirdFailureAction\" -parameterDescription \"Third Failure Action\" -default \"restart\"\n    $firstFailureDelay = Get-OctoParameter -parameterName \"FirstFailureDelay\" -parameterDescription \"First Failure Delay\" -default 180000\n    $secondFailureDelay = Get-OctoParameter -parameterName \"SecondFailureDelay\" -parameterDescription \"Second Failure Delay\" -default 180000\n    $thirdFailureDelay = Get-OctoParameter -parameterName \"ThirdFailureDelay\" -parameterDescription \"Third Failure Delay\" -default 180000\n    $reset = Get-OctoParameter -parameterName \"Reset\" -parameterDescription \"Reset\" -default 86400\n\n    $service = Get-Service $serviceName -ErrorAction SilentlyContinue\n\n    if (!$service)\n    {\n        Write-Host \"Windows Service '$serviceName' not found, skipping.\"\n        return\n    }\n\n    echo \"Updating the '$serviceName' service with recovery options...\"\n    echo \"    On first failure '$firstFailureAction' after '$firstFailureDelay' milliseconds.\"\n    echo \"    On second failure '$secondFailureAction' after '$secondFailureDelay' milliseconds.\"\n    echo \"    On third failure '$thirdFailureAction' after '$thirdFailureDelay' milliseconds.\"\n    echo \"    Reset after '$reset' minutes.\"\n\n    sc.exe failure $service.Name actions= $firstFailureAction/$firstFailureDelay/$secondFailureAction/$secondFailureDelay/$thirdFailureAction/$thirdFailureDelay reset= $reset\n\n    echo \"Done\"\n}\n\nfunction Get-OctoParameter() \n{\n    Param\n    (\n        [Parameter(Mandatory=$true)]$parameterName,\n        [Parameter(Mandatory=$true)]$parameterDescription,\n        [Parameter(Mandatory=$false)]$default\n    )\n\n    $ErrorActionPreference = \"SilentlyContinue\" \n    $value = $OctopusParameters[$parameterName] \n    $ErrorActionPreference = \"Stop\" \n    \n    if (! $value) \n    {\n        if(! $default) \n        {\n            throw \"'$parameterDescription' cannot be empty, please specify a value.\"\n        }\n\n        return $default\n    }\n    \n    return $value\n}\n\nmain"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-service-set-recovery-on-failure-actions.json",
  "Website": "/step-templates/5576faaf-e024-4248-ad98-41717a7c4f43",
  "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 Tuesday, April 11, 2017