Final touches

pull/410/head
romanitho 2023-10-09 12:52:03 +02:00
parent e42a8cedaa
commit 8835de9f90
2 changed files with 20 additions and 16 deletions

View File

@ -311,6 +311,18 @@ if (Test-Network) {
#Run WAU in user context if feature is activated
if ($WAUConfig.WAU_UserContext -eq 1) {
#Create User context task if not existing
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue
if (!$UserContextTask) {
#Create the scheduled task in User context
$taskAction = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-upgrade.ps1`"`""
$taskUserPrincipal = New-ScheduledTaskPrincipal -GroupId S-1-5-11
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath 'WAU' -InputObject $task -Force | Out-Null
Write-ToLog "-> User Context task created."
}
#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) {

View File

@ -186,26 +186,18 @@ function Invoke-PostUpdateActions {
}
}
#Create User context task if not existing and manage its activation
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue
if (!$UserContextTask) {
#Create the scheduled task in User context
$taskAction = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-upgrade.ps1`"`""
$taskUserPrincipal = New-ScheduledTaskPrincipal -GroupId S-1-5-11
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath 'WAU' -InputObject $task -Force | Out-Null
#Activate WAU in user context if previously configured (as "Winget-AutoUpdate-UserContext" at root)
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath '\' -ErrorAction SilentlyContinue
if ($UserContextTask) {
#Remove Winget-AutoUpdate-UserContext at root.
Unregister-ScheduledTask $UserContextTask -Confirm:$False
#If scheduled task didn't exist, user context was not activated. Set it in registry.
New-ItemProperty $regPath -Name WAU_UserContext -Value 0 -PropertyType DWord -Force | Out-Null
Write-ToLog "-> User Context task created and set to 'disabled' in registry."
}
#If scheduled task exists but no WAU_UserContext setting, user context was activated. Set it in registry.
elseif (!($WAUConfig.WAU_UserContext)) {
#Set it in registry as activated.
New-ItemProperty $regPath -Name WAU_UserContext -Value 1 -PropertyType DWord -Force | Out-Null
Write-ToLog "-> User Context task existes and set to 'enabled' in registry."
Write-ToLog "-> Old User Context task deleted and set to 'enabled' in registry."
}
### End of post update actions ###
#Reset WAU_UpdatePostActions Value