From a7c1e01dd6109bfae509ba06c8dfede29300b600 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 1 Feb 2023 04:06:17 +0100 Subject: [PATCH] Check if any user is logged on --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 39 ++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index aef40ce..351ced8 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -271,21 +271,34 @@ if (Test-Network) { Write-Log "No new update." "Green" } - #Run WAU in user context if currently as system and the user task exist - $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue - if ($IsSystem -and $UserScheduledTask) { + #Check if any user is logged on if System and run User task (if installed) + if ($IsSystem) { + #User check routine from: https://stackoverflow.com/questions/23219718/powershell-script-to-see-currently-logged-in-users-domain-and-machine-status + $explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue) + If ($explorerprocesses.Count -eq 0) + { + Write-Log "No explorer process found / Nobody interactively logged on.. ..exiting" + Exit 0 + } + Else + { + #Run WAU in user context if the user task exist + $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue + if ($UserScheduledTask) { - #Get Winget system apps to excape them befor running user context - Write-Log "Get list of installed Winget apps in System context..." - Get-WingetSystemApps + #Get Winget system apps to excape them befor running user context + Write-Log "User logged on, get a list of installed Winget apps in System context..." + Get-WingetSystemApps - #Run user context scheduled task - Write-Log "Starting WAU in User context" - Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue - Exit 0 - } - elseif (!$UserScheduledTask){ - Write-Log "User context execution not installed" + #Run user context scheduled task + Write-Log "Starting WAU in User context" + Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue + Exit 0 + } + elseif (!$UserScheduledTask){ + Write-Log "User context execution not installed" + } + } } } else {