diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index b65e17d..2e736b8 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -328,13 +328,7 @@ function Install-WingetAutoUpdate { Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False # Settings for the scheduled task for Updates (System) - $ServiceUI = Test-Path "$PSScriptRoot\Winget-AutoUpdate\ServiceUI.exe" - if ($ServiceUI) { - $taskAction = New-ScheduledTaskAction -Execute "$WingetUpdatePath\ServiceUI.exe" -Argument "-process:explorer.exe %windir%\System32\wscript.exe \`"$WingetUpdatePath\Invisible.vbs \`" \`"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \`"\`"$WingetUpdatePath\winget-upgrade.ps1\`"\`"\`"" - } - else { - $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`"" - } + $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`"" $taskTriggers = @() if ($UpdatesAtLogon) { $tasktriggers += New-ScheduledTaskTrigger -AtLogOn diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index e904e4d..2ebc584 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -17,7 +17,31 @@ $Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsS #Log initialisation $LogFile = "$WorkingDir\logs\updates.log" -Write-ToLog -LogMsg "CHECK FOR APP UPDATES" -IsHeader + +if ($IsSystem) { + #Check if any connected user when running as system + $explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue) + #Check if ServiceUI exists + $ServiceUI = Test-Path "$WorkingDir\ServiceUI.exe" + If ($explorerprocesses.Count -gt 0 -and $ServiceUI) { + #User connected, Check for current session ID (O = system) + $SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId + if ($SessionID -eq 0) { + #Rerun WAU in system context with ServiceUI + & $WorkingDir\ServiceUI.exe -process:explorer.exe $env:windir\System32\wscript.exe \`"$WorkingDir\Invisible.vbs\`" \`"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \`"\`"$WorkingDir\winget-upgrade.ps1\`"\`"\`" + Exit 0 + } + else { + Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context with ServiceUI)" -IsHeader + } + } + else { + Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader + } +} +else { + Write-ToLog -LogMsg "CHECK FOR APP UPDATES (User context)" -IsHeader +} #Get settings and Domain/Local Policies (GPO) if activated. $Script:WAUConfig = Get-WAUConfig @@ -27,7 +51,6 @@ if ($($WAUPolicies.WAU_ActivateGPOManagement -eq 1)) { #Log running context and more... if ($IsSystem) { - Write-ToLog "Running in System context" # Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files. $MaxLogFiles = $WAUConfig.WAU_MaxLogFiles @@ -61,9 +84,6 @@ if ($IsSystem) { $SettingsPath = "$Env:windir\system32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\defaultState\settings.json" Add-ScopeMachine $SettingsPath } -else { - Write-ToLog "Running in User context" -} #Get Notif Locale function $LocaleDisplayName = Get-NotifLocale diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 30d3720..eb52cb9 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -197,18 +197,6 @@ function Invoke-PostUpdateActions { Write-ToLog "-> Policies task created." } - #Set WAU scheduled task with ServiceUI - $WAUTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate' -ErrorAction SilentlyContinue - $ServiceUI = Test-Path "$($WorkingDir)\ServiceUI.exe" - if ($ServiceUI) { - $taskAction = New-ScheduledTaskAction -Execute "$($WorkingDir)\ServiceUI.exe" -Argument "-process:explorer.exe %windir%\System32\wscript.exe \`"$($WorkingDir)\Invisible.vbs \`" \`"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \`"\`"$WingetUpdatePath\winget-upgrade.ps1\`"\`"\`"" - Write-ToLog "-> ServiceUI enabled." - } - else { - $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WorkingDir)\winget-upgrade.ps1`"" - } - Set-ScheduledTask -TaskPath $WAUTask.TaskPath -TaskName $WAUTask.TaskName -Action $taskAction | Out-Null - ### End of post update actions ### diff --git a/Winget-AutoUpdate/functions/Write-ToLog.ps1 b/Winget-AutoUpdate/functions/Write-ToLog.ps1 index 976a639..5643832 100644 --- a/Winget-AutoUpdate/functions/Write-ToLog.ps1 +++ b/Winget-AutoUpdate/functions/Write-ToLog.ps1 @@ -26,7 +26,7 @@ function Write-ToLog { #If header requested if ($IsHeader) { - $Log = " `n##################################################`n# $LogMsg - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" + $Log = " `n########################################################`n# $LogMsg - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n########################################################" } else { $Log = "$(Get-Date -UFormat "%T") - $LogMsg"