From 43677b84d518da853000dc61e778a76047885ea1 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Thu, 14 Dec 2017 17:18:22 +0100 Subject: [PATCH] Don't require the service to run upfront, remember its state instead. And only start the service eventually if it was running before. --- Updater/Update-Service.ps1 | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Updater/Update-Service.ps1 b/Updater/Update-Service.ps1 index 41ef792..e4d0946 100644 --- a/Updater/Update-Service.ps1 +++ b/Updater/Update-Service.ps1 @@ -14,25 +14,19 @@ Param( ### function definitions ##################################################### -function Ensure-ServiceRunning([string]$ServiceName) { - $Continue = $True +function ServiceIsRunning([string]$ServiceName) { try { $Service = Get-Service $ServiceName -ErrorAction Stop if ($Service.Status -ne "Running") { - Write-Host "Service $($ServiceName) is not running." - $Continue = $False + Log-Debug "Service $($ServiceName) is not running." + Return $False } } catch { - Write-Host $_.Exception.Message - $Continue = $False - } - if ($Continue) { - Log-Debug "Prerequisites okay, service is running." - Return + Log-Error "Error checking service state: $($_.Exception.Message)" + Exit } - Log-Error "ERROR: Service '$($ServiceName)' must be installed and running." - Exit + Return $True } @@ -414,7 +408,7 @@ Log-Debug "$($Me) started..." # first check if the service is installed and running at all -Ensure-ServiceRunning $ServiceName +$ServiceRunningBefore = ServiceIsRunning $ServiceName $UpdPathConfig = "$($UpdateSourcePath)\Configs\$($env:COMPUTERNAME)" $UpdPathConfigCommon = "$($UpdateSourcePath)\Configs\_COMMON_" @@ -449,8 +443,12 @@ if ($ServiceUpdated) { } if ($msg -ne "") { - Log-Debug "Update action occurred, finishing up..." - Start-MyService + if ($ServiceRunningBefore) { + Log-Debug "Update action occurred, finishing up..." + Start-MyService + } else { + Log-Debug "Not starting the service as it was not running before." + } Send-MailReport -Subject "Config and / or service has been updated!" ` -Body $msg } else { -- GitLab