From ab215d86157e8433a2d13069045e6f7a36cb41fa Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Sat, 31 Dec 2022 00:59:46 +0100 Subject: [PATCH] UserContext and fixes to settings --- Policies/WAU.admx | 10 ++++++++++ Policies/en-US/WAU.adml | 4 ++++ Winget-AutoUpdate/functions/Get-Policies.ps1 | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/Policies/WAU.admx b/Policies/WAU.admx index e155d37..c40bf32 100644 --- a/Policies/WAU.admx +++ b/Policies/WAU.admx @@ -288,6 +288,16 @@ + + + + + + + + + + diff --git a/Policies/en-US/WAU.adml b/Policies/en-US/WAU.adml index 566b670..558a1c9 100644 --- a/Policies/en-US/WAU.adml +++ b/Policies/en-US/WAU.adml @@ -97,6 +97,10 @@ If this policy is not configured or disabled, Updates at Time: (06:00 AM).22:00 23:00 24:00 + User context execution + This policy setting specifies whether to enable User context execution or not . + +If this policy is disabled or not configured, the default is No. diff --git a/Winget-AutoUpdate/functions/Get-Policies.ps1 b/Winget-AutoUpdate/functions/Get-Policies.ps1 index 0761cb3..0096eac 100644 --- a/Winget-AutoUpdate/functions/Get-Policies.ps1 +++ b/Winget-AutoUpdate/functions/Get-Policies.ps1 @@ -237,6 +237,25 @@ Function Get-Policies { $ChangedSettings++ } + if ($null -ne $($WAUPolicies.WAU_UserContext) -and ($($WAUPolicies.WAU_UserContext) -ne $($WAUConfig.WAU_UserContext))) { + #Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False + New-ItemProperty $regPath -Name WAU_UserContext -Value $($WAUPolicies.WAU_UserContext) -PropertyType DWord -Force | Out-Null + # Settings for 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 + + # Set up the task for user apps + $task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings + Register-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -InputObject $task -Force | Out-Null + $ChangedSettings++ + } + elseif ($null -eq $($WAUPolicies.WAU_UserContext) -and ($($WAUConfig.WAU_UserContext) -or $($WAUConfig.WAU_UserContext) -eq 0)) { + Remove-ItemProperty $regPath -Name WAU_UserContext -Force -ErrorAction SilentlyContinue | Out-Null + Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False + $ChangedSettings++ + } + if ($ChangedSettings -gt 0) { Write-Log "Changed settings: $ChangedSettings" "Yellow" }