Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions step-templates/windows-service-stop-or-kill.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"Name": "Stop Service With Kill",
"Description": "This steps stops the specified service and in case it does not respond or times out, the service will be killed.",
"ActionType": "Octopus.Script",
"Version": 8,
"Version": 9,
"CommunityActionTemplateId": null,
"Packages": [],
"Properties": {
"Octopus.Action.Script.ScriptBody": "$svcName = $OctopusParameters['ServiceName']\n$svcTimeout = $OctopusParameters['ServiceStopTimeout']\n\nfunction Stop-ServiceWithTimeout ([string] $name, [int] $timeoutSeconds) {\n $timespan = New-Object -TypeName System.Timespan -ArgumentList 0,0,$timeoutSeconds\n\n If ($svc = Get-Service $svcName -ErrorAction SilentlyContinue) {\n if ($svc -eq $null) { return $true }\n if ($svc.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped) { return $true }\n $svc.Stop()\n try {\n Write-Host \"Stopping Service\" $svcTimeout \"Timeout\"\n $svc.WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped, $timespan)\n }\n catch [ServiceProcess.TimeoutException] {\n Write-Host \"Timeout stopping service $($svc.Name)\"\n return $false\n }\n Write-Host \"Service Sucessfully stopped\"\n return $true\n\n } Else {\n Write-Host \"Service does not exist, this is acceptable. Probably the first time deploying to this target\"\n Exit\n }\n}\n\nWrite-Host \"Checking service\"\n\n$svcpid = (get-wmiobject Win32_Service | where{$_.Name -eq $svcName}).ProcessId\nWrite-Host \"Found PID \" + $svcpid \n\nStop-ServiceWithTimeout -name $svcName -timeoutSeconds $svcTimeout\n\nWrite-Host \"Rechecking service\"\n$svcpid = (get-wmiobject Win32_Service | where{$_.Name -eq $svcName}).ProcessId\nWrite-Host \"Found PID \" + $svcpid \n\n$service = Get-Service -name $svcName | Select -Property Status\nif($service.Status -ne \"Stopped\"){\tStart-Sleep -seconds 5 }\n\n#Check-Service process \nif($svcpid){\n #still exists?\n $p = get-process -id $svcpid -ErrorAction SilentlyContinue\n if($p){\n Write-Host \"Killing Service\"\n Stop-Process $p.Id -force\n }\n}",
"Octopus.Action.Script.ScriptBody": "$svcName = $OctopusParameters['ServiceName']\n$svcTimeout = $OctopusParameters['ServiceStopTimeout']\n\nfunction Stop-ServiceWithTimeout ([string] $name, [int] $timeoutSeconds) {\n $timespan = New-Object -TypeName System.Timespan -ArgumentList 0,0,$timeoutSeconds\n\n If ($svc = Get-Service $svcName -ErrorAction SilentlyContinue) {\n if ($null -eq $svc) { return $true }\n if ($svc.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped) { return $true }\n try {\n Write-Host \"Stopping Service with Timeout\" $svcTimeout \"seconds\"\n $svc.Stop()\n $svc.WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped, $timespan)\n }\n catch [ServiceProcess.TimeoutException] {\n Write-Host \"Timeout stopping service $($svc.Name)\"\n return $false\n }\n catch {\n Write-Warning \"Service $svcName could not be stopped: $_\"\n }\n Write-Host \"Service Sucessfully stopped\"\n\n } Else {\n Write-Host \"Service does not exist, this is acceptable. Probably the first time deploying to this target\"\n Exit\n }\n}\n\nWrite-Host \"Checking service $svcName\"\ntry {\n $svc = Get-Service $svcName\n}\ncatch {\n if ($null -eq $svc) { Write-Warning \"Service $svcName not found.\" }\n exit 1\n}\n\n$svcpid1 = (get-wmiobject Win32_Service | Where-Object{$_.Name -eq $svcName}).ProcessId\nif($svcpid1 -ne 0) {\n Write-Host \"Found PID $svcpid1 - stopping service now...\"\n Stop-ServiceWithTimeout -name $svcName -timeoutSeconds $svcTimeout\n}\nelse {\n Write-Host \"No PID found for $svcName - service is already stopped.\"\n exit 0\n}\n\nWrite-Host \"Rechecking service\"\n$svcpid2 = (get-wmiobject Win32_Service | Where-Object{$_.Name -eq $svcName}).ProcessId\nif($svcpid2 -eq 0) {\n Write-Host \"no PID found for $svcName\"\n}\nelse {\n Write-Warning \"PID $svcpid2 found for $svcName - service not stopped. Trying to Kill the process.\"\n}\n\n$service = Get-Service -name $svcName | Select-Object -Property Status\nif($service.Status -ne \"Stopped\"){\n Start-Sleep -seconds 5\n $p = get-process -id $svcpid2 -ErrorAction SilentlyContinue\n if($p){\n Write-Host \"Killing PID\" $p.id \"(\" $p.Name \")\"\n try {\n Stop-Process $p.Id -force\n }\n catch {\n Write-Warning \"process\" $p.id \"could not be stopped:\" $_\n }\n }\n}\n",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline"
},
Expand All @@ -33,12 +33,12 @@
}
}
],
"LastModifiedOn": "2018-11-05T03:59:57.028Z",
"LastModifiedBy": "benjimac93",
"LastModifiedOn": "2025-02-20T16:15:00Z",
"LastModifiedBy": "Bjoern-Hennings",
"$Meta": {
"ExportedAt": "2018-11-05T03:59:57.028Z",
"ExportedAt": "2018-11-05T03:59:57.0280000Z",
"OctopusVersion": "2018.8.12",
"Type": "ActionTemplate"
},
"Category": "windows"
}
}