Browse Source

onShutdown.ps1: added check if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed.

FloKra 2 years ago
parent
commit
958c653cf9
2 changed files with 21 additions and 7 deletions
  1. 4 1
      MQTTmsgOnShutdown/README.md
  2. 17 6
      MQTTmsgOnShutdown/onShutdown.ps1

+ 4 - 1
MQTTmsgOnShutdown/README.md

@@ -20,12 +20,15 @@ Intended to switch off a Tasmota plug, delayed, after shutdown, but NOT on resta
 This script checks if there is a Event 1074 (source: User32) in the System Event Log, that is less than 15 seconds old. 
 If so, it checks the content of this event, which contains the info if we are about to "power off" or "restart". 
 
-The script must be set as shutdown script via Windows Group Policy (local GPO).
+The script must be set as shutdown script via Windows Group Policy (local GPO). 
+
+V2 now checks if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed. 
 
 
 
 ## Setup
 
+- install PSWindowsUpdate (PS as Admin): `Install-Module -Name PSWindowsUpdate`
 - copy script to C:\scripts\onShutdown.ps1
 - edit topics, MQTT host, etc.
 - Start -> Run -> gpedit.msc

+ 17 - 6
MQTTmsgOnShutdown/onShutdown.ps1

@@ -8,6 +8,9 @@
 # - download Mosquitto Windows x64 installer
 # - extract and copy mosquitto_pub.exe, mosquitto.dll, libssl-1_1-x64.dll and libcrypto-1_1-x64.dll to
 #   c:\Tools\mosquitto (or anywhere and change $mosquittoPubPath accordingly)
+# - Windows Update PowerShell Module:
+#   http://woshub.com/pswindowsupdate-module/
+#   (as Admin): Install-Module -Name PSWindowsUpdate
 #
 # This script checks if there is a Event 1074 (source: User32) in the System Event Log, that is
 # less than 15 seconds old. 
@@ -30,14 +33,14 @@ $logFile = "C:\LOG\onShutdown_PS.log"
 
 $mosquittoPubPath = "c:\Tools\mosquitto\mosquitto_pub.exe"
 $mqttHost = "mqtt.lan"
-#$mqttUser = "username"
-#$mqttPwd = "password"
+$mqttUser = "script"
+$mqttPwd = "rlAzqusqfbAy"
 
 # Topic to send status info to (SHUTDOWN or RESTART)
-$mqttStatTopic = "MediaPC/ShutdownEvent"
+$mqttStatTopic = "MediaPC-SZ/ShutdownEvent"
 
 # Topic/Payload on Shutdown (i.E. send an Event to a Tasmota device)
-$mqttTopicOnShutdown = "cmnd/TasmotaMediaPC/event"
+$mqttTopicOnShutdown = "cmnd/T5SZ-Media-Pwr/event"
 $mqttPayloadOnShutdown = "delayedOff"
 
 # Topic/Payload on Restart (i.E. send an Event to a Tasmota device)
@@ -56,20 +59,28 @@ if($mqttUser -and $mqttPwd -and $mqttUser -ne "" -and $mqttPwd -ne "") {
 $logDate = "{0:yyyy-MM-dd HH:mm:ss}" -f (Get-Date)
 if($logFile) { Write-Output $logDate >> $logFile }
 
+
+$WURebootRequired = Get-WURebootStatus.RebootRequired
+
 Get-WinEvent -FilterHashtable @{logname='System'; id=1074} -MaxEvents 1 | ForEach-Object {
     $ts_event = [datetime]$_.TimeCreated
     $ts_delta = (New-TimeSpan -Start $ts_event).TotalSeconds
     $ts_str = $ts_delta.ToString("#")
 	$command = $_.Properties[4].Value
     
-    if($logFile) { Write-Output "  last shutdown-event was '$command', $ts_str s ago" >> $logFile }
+    if($logFile) { 
+		Write-Output "  last shutdown-event was '$command', $ts_str s ago" >> $logFile 
+		if($WURebootRequired) {
+				Write-Output "  Windows Update reboot is required. As in this case the update will install on shutdown, Powerdown-Command to Tasmota is omitted this time." >> $logFile 
+			}
+		}
 
 	if($ts_delta -lt 15) {
 		if($logFile) { Write-Output "    ==> OK, publish MQTT messages." >> $logFile }
         if($command -eq "power off") {
 			$cmdLine = "$mosquittoPubPath -h $mqttHost $mosquittoPubUserPassword-t $mqttStatTopic -m SHUTDOWN"
 			Invoke-Expression $cmdLine
-			if($mqttTopicOnShutdown -and $mqttTopicOnShutdown -ne "") {
+			if(-not $WURebootRequired -and $mqttTopicOnShutdown -and $mqttTopicOnShutdown -ne "") {
 				$cmdLine = "$mosquittoPubPath -h $mqttHost $mosquittoPubUserPassword-t $mqttTopicOnShutdown -m $mqttPayloadOnShutdown"
 				Invoke-Expression $cmdLine
 			}