From 04bc02d9df6e0faa26adc43e9e4087dfeafbfd6e Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 8 Dec 2017 14:56:28 +0100
Subject: [PATCH] Send mails on service shutdown / startup errors.

Refers to #13
---
 AutoTx-Updater/Update-Service.ps1 | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/AutoTx-Updater/Update-Service.ps1 b/AutoTx-Updater/Update-Service.ps1
index 2c11a24..bddcd37 100644
--- a/AutoTx-Updater/Update-Service.ps1
+++ b/AutoTx-Updater/Update-Service.ps1
@@ -59,11 +59,14 @@ function Exit-IfDirMissing([string]$DirName, [string]$Desc) {
 function Stop-MyService {
     Write-Host -NoNewLine "Stopping service $($ServiceName): "
     try {
-        Stop-Service $ServiceName -ErrorAction Stop
+        Stop-Service "$($ServiceName)" -ErrorAction Stop
         Write-Host "[OK]" -Fore Green
     }
     catch {
+        $ex = $_.Exception.Message
         Write-Host "[FAILED]" -Fore Red
+        Send-MailReport -Subject "Shutdown of service $($ServiceName) failed!" `
+            -Body "Trying to stop the service results in this error:`n$($ex)"
         Exit
     }
 }
@@ -75,13 +78,16 @@ function Start-MyService {
     }
     Write-Host -NoNewLine "Starting service $($ServiceName): "
     try {
-        Start-Service $ServiceName -ErrorAction Stop
+        Start-Service "$($ServiceName)" -ErrorAction Stop
         Write-Host "[OK]" -Fore Green
     }
     catch {
-        $ex = $_.Exception
+        $ex = $_.Exception.Message
         Write-Host "[FAILED]" -Fore Red
-        Write-Host $ex.Message
+        Write-Host $ex
+        Send-MailReport -Subject "Startup of service $($ServiceName) failed!" `
+            -Body "Trying to start the service results in this error:`n$($ex)"        
+        Exit
     }
 }
 
-- 
GitLab