From c8553828b5fe2a3c1056611ddad158cb38404bf5 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Thu, 1 Mar 2018 14:48:07 +0100
Subject: [PATCH] Add ForceServiceCleanup switch to Update-Service.ps1.

Using this option will shut down the service independent of its state,
remove the status XML file (will be re-created by the service during
the next start) and (re-) starts the service no matter if it was running
before.
---
 Updater/Update-Service.ps1 | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/Updater/Update-Service.ps1 b/Updater/Update-Service.ps1
index 64f0cad..044f522 100644
--- a/Updater/Update-Service.ps1
+++ b/Updater/Update-Service.ps1
@@ -7,7 +7,8 @@
 
 [CmdletBinding()]
 Param(
-    [Parameter(Mandatory=$True)][string] $UpdaterSettings
+    [Parameter(Mandatory=$True)][string] $UpdaterSettings,
+    [Parameter(Mandatory=$False)][switch] $ForceServiceCleanup
 )
 
 
@@ -94,11 +95,13 @@ function Stop-MyService([string]$Message) {
         Log-Info "$($Message) (Service already in state 'Stopped')"
         Return
     }
-    if (ServiceIsBusy) {
-        $msg = "*DENYING* to stop the service $($ServiceName) as it is "
-        $msg += "currently busy.`nShutdown reason was '$($Message)'."
-        Log-Info $msg
-        Exit
+    if (-Not $ForceServiceCleanup){
+        if (ServiceIsBusy) {
+            $msg = "*DENYING* to stop the service $($ServiceName) as it is "
+            $msg += "currently busy.`nShutdown reason was '$($Message)'."
+            Log-Info $msg
+            Exit
+        }
     }
     try {
         Log-Info "$($Message) Attempting service $($ServiceName) shutdown..."
@@ -392,6 +395,13 @@ function Update-ServiceBinaries {
         Log-Error "Creating [$($MarkerFile)] FAILED:`n> $($_.Exception.Message)"
         Return $False
     }
+
+    if (-Not $ForceServiceCleanup) {
+        Return $True
+    }
+
+    Log-Debug "<ForceServiceCleanup> removing status file [$($StatusXml)]"
+    Remove-Item -Force $StatusXml
     Return $True
 }
 
@@ -643,8 +653,8 @@ try {
 
 
 
-    if ($ServiceRunningBefore) {
-        Log-Debug "$($UpdSummary) Trying to start the service again..."
+    if ($ServiceRunningBefore -Or $ForceServiceCleanup) {
+        Log-Debug "$($UpdSummary) Trying to start the service..."
         Start-MyService
     } else {
         Log-Debug "$($UpdSummary) Leaving the service stopped, as before."
-- 
GitLab