From edca69b8117385e298cc0c96f34a9276fd739c90 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Tue, 12 Dec 2017 16:42:38 +0100
Subject: [PATCH] Try to show log message when service startup fails.

Refers to #13
---
 AutoTx-Updater/Update-Service.ps1 | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/AutoTx-Updater/Update-Service.ps1 b/AutoTx-Updater/Update-Service.ps1
index 82fc5e3..65be7c3 100644
--- a/AutoTx-Updater/Update-Service.ps1
+++ b/AutoTx-Updater/Update-Service.ps1
@@ -56,6 +56,20 @@ function Exit-IfDirMissing([string]$DirName, [string]$Desc) {
 }
 
 
+function Get-LastLogLines([string]$Path, [int]$Count) {
+    try {
+        $msg = Get-Content -Path $Path -Tail $Count -ErrorAction Stop
+    }
+    catch {
+        $ex = $_.Exception.Message
+        $errxx = "XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX"
+        $msg = "`n$errxx`n`n$ex `n`n$errxx"
+    }
+    # Out-String is required as otherwise all newlines from the log disappear:
+    Return $msg | Out-String
+}
+
+
 function Stop-MyService {
     try {
         Stop-Service "$($ServiceName)" -ErrorAction Stop
@@ -80,8 +94,12 @@ function Start-MyService {
     }
     catch {
         $ex = $_.Exception.Message
+        $msg = "Trying to start the service results in this error:`n$($ex)`n`n"
+        $msg += " -------------------- last 50 log lines --------------------`n"
+        $msg += Get-LastLogLines "$($LogPath)\service.log" 50
+        $msg += " -------------------- ----------------- --------------------`n"
         Send-MailReport -Subject "Startup of service $($ServiceName) failed!" `
-            -Body "Trying to start the service results in this error:`n$($ex)"        
+            -Body $msg
         Exit
     }
 }
-- 
GitLab