|
@@ -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
|
|
|
}
|