Check if any user is logged on

pull/270/head
KnifMelti 2023-02-01 04:06:17 +01:00
parent b248782c5c
commit a7c1e01dd6
1 changed files with 26 additions and 13 deletions

View File

@ -271,12 +271,23 @@ if (Test-Network) {
Write-Log "No new update." "Green" Write-Log "No new update." "Green"
} }
#Run WAU in user context if currently as system and the user task exist #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 $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue
if ($IsSystem -and $UserScheduledTask) { if ($UserScheduledTask) {
#Get Winget system apps to excape them befor running user context #Get Winget system apps to excape them befor running user context
Write-Log "Get list of installed Winget apps in System context..." Write-Log "User logged on, get a list of installed Winget apps in System context..."
Get-WingetSystemApps Get-WingetSystemApps
#Run user context scheduled task #Run user context scheduled task
@ -288,6 +299,8 @@ if (Test-Network) {
Write-Log "User context execution not installed" Write-Log "User context execution not installed"
} }
} }
}
}
else { else {
Write-Log "Critical: Winget not installed or detected, exiting..." "red" Write-Log "Critical: Winget not installed or detected, exiting..." "red"
New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected" -Force New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected" -Force