Windows Service - Check status

Octopus.Script exported 2018-09-27 by fedelemattia belongs to ‘Windows’ category.

check Windows service status on target machine or remote host(s).

for the remote host(s): the ‘OctopusDeploy Tentacle’ service installed on target machine (or main tentacle) must have the grants for remote query

Parameters

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

Windows Service Name

servicenamewin =

null

Desired state

servicewinstatus = Running

null

Comuputer

winhostlist =

Host or Hosts list comma-separated:

  • machine123
  • machine123,machine124,machine125

Script body

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

###----------------------------###
###                            ###
###check Windows service status###
###                            ###
###----------------------------###

$servicename=$OctopusParameters['servicenamewin']
$desiredstate=$OctopusParameters['servicewinstatus']
$hostslist=$OctopusParameters['winhostlist']

###hosts list comma-separated or single host
$hostl=$hostslist.Split(",")
$hostl

foreach($h in $hostl){
    Write-Output "Running on $h"
    #check the status of the service on remote host
    $remotestatus=invoke-command -computername $h {(Get-Service -Name $servicename).Status}
    $status=$remotestatus.Value
    if($status){
        try{
            if($status -like $desiredstate){
                Write-Output "The service $servicename is correctly $desiredstate on $h"
            }
            else{
                Write-Error "The service $servicename is NOT $desiredstate. Currently state is $status"
            
            }
        }
        catch{
            Write-Error "Is not possible to determinate the status for service $servicename"
        }

    }
    else{
        Write-Error "Error on retrieving the status. Invalid service name or host $h"
    }
}

###@author:fedele_mattia

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": "feacd1dd-715b-4176-af87-73a417daeb75",
  "Name": "Windows Service - Check status",
  "Description": "check Windows service status on target machine or remote host(s).\n\n_for the remote host(s): the 'OctopusDeploy Tentacle' service installed on target machine (or main tentacle) must have the grants for remote query_\n",
  "Version": 20,
  "ExportedAt": "2018-09-27T08:29:15.090Z",
  "ActionType": "Octopus.Script",
  "Author": "fedelemattia",
  "Parameters": [
    {
      "Id": "01a19172-fb06-40e2-8bb1-bcd6b9fe360b",
      "Name": "servicenamewin",
      "Label": "Windows Service Name",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "3e9bc02a-ea38-4336-8e81-0252a0a91ff4",
      "Name": "servicewinstatus",
      "Label": "Desired state",
      "HelpText": null,
      "DefaultValue": "Running",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "Running|Running\nStopped|Stopped"
      }
    },
    {
      "Id": "828944cb-ff12-47f7-bf4b-fb6d1799235d",
      "Name": "winhostlist",
      "Label": "Comuputer",
      "HelpText": "Host or Hosts list **comma-separated**:\n- _machine123_\n- _machine123,machine124,machine125_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "###----------------------------###\n###                            ###\n###check Windows service status###\n###                            ###\n###----------------------------###\n\n$servicename=$OctopusParameters['servicenamewin']\n$desiredstate=$OctopusParameters['servicewinstatus']\n$hostslist=$OctopusParameters['winhostlist']\n\n###hosts list comma-separated or single host\n$hostl=$hostslist.Split(\",\")\n$hostl\n\nforeach($h in $hostl){\n    Write-Output \"Running on $h\"\n    #check the status of the service on remote host\n    $remotestatus=invoke-command -computername $h {(Get-Service -Name $servicename).Status}\n    $status=$remotestatus.Value\n    if($status){\n        try{\n            if($status -like $desiredstate){\n                Write-Output \"The service $servicename is correctly $desiredstate on $h\"\n            }\n            else{\n                Write-Error \"The service $servicename is NOT $desiredstate. Currently state is $status\"\n            \n            }\n        }\n        catch{\n            Write-Error \"Is not possible to determinate the status for service $servicename\"\n        }\n\n    }\n    else{\n        Write-Error \"Error on retrieving the status. Invalid service name or host $h\"\n    }\n}\n\n###@author:fedele_mattia"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-service-check-status.json",
  "Website": "/step-templates/feacd1dd-715b-4176-af87-73a417daeb75",
  "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 Thursday, September 27, 2018