11<#
22. SYNOPSIS
3- Handle GPO/Polices
3+ Applies Group Policy settings to WAU scheduled tasks.
44
55. DESCRIPTION
6- Daily update settings from policies
6+ Reads WAU configuration from GPO registry keys and updates the
7+ Winget-AutoUpdate scheduled task triggers accordingly.
8+ Handles daily, bi-daily, weekly, bi-weekly, and monthly schedules,
9+ as well as logon triggers and time delays.
10+
11+ . NOTES
12+ This script is executed by the WAU-Policies scheduled task.
13+ GPO registry path: HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate
14+ Logs applied settings to: logs\LatestAppliedSettings.txt
715#>
816
9- # Import functions
17+ # Import configuration function
1018. " $PSScriptRoot \functions\Get-WAUConfig.ps1"
1119
12- # Check if GPO Management is detected
20+ # Check if GPO management is enabled
1321$GPOManagementDetected = Get-ItemProperty " HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate" - ErrorAction SilentlyContinue
1422
1523if ($GPOManagementDetected ) {
1624
17- # Get WAU settings
25+ # Load WAU configuration (with GPO overrides)
1826 $WAUConfig = Get-WAUConfig
1927
20- # Log init
28+ # Initialize logging
2129 $GPOLogDirectory = Join-Path - Path $WAUConfig.InstallLocation - ChildPath " logs"
2230 if (! (Test-Path - Path $GPOLogDirectory )) {
2331 New-Item - ItemType Directory - Path $GPOLogDirectory - Force | Out-Null
2432 }
2533 $GPOLogFile = Join-Path - Path $GPOLogDirectory - ChildPath " LatestAppliedSettings.txt"
2634 Set-Content - Path $GPOLogFile - Value " ### POLICY CYCLE - $ ( Get-Date ) ###`n "
2735
28- # Get Winget-AutoUpdate scheduled task
36+ # Get current scheduled task configuration
2937 $WAUTask = Get-ScheduledTask - TaskName ' Winget-AutoUpdate' - ErrorAction SilentlyContinue
30-
31- # Update 'Winget-AutoUpdate' scheduled task settings
3238 $currentTriggers = $WAUTask.Triggers
3339 $configChanged = $false
3440
35- # Check if LogOn trigger setting has changed
41+ # === Check if LogOn trigger setting has changed ===
3642 $hasLogonTrigger = $currentTriggers | Where-Object { $_.CimClass.CimClassName -eq " MSFT_TaskLogonTrigger" }
3743 if (($WAUConfig.WAU_UpdatesAtLogon -eq 1 -and -not $hasLogonTrigger ) -or
3844 ($WAUConfig.WAU_UpdatesAtLogon -ne 1 -and $hasLogonTrigger )) {
3945 $configChanged = $true
4046 }
4147
42- # Check if schedule type has changed
48+ # === Detect current schedule type ===
4349 $currentIntervalType = " None"
4450 foreach ($trigger in $currentTriggers ) {
4551 if ($trigger.CimClass.CimClassName -eq " MSFT_TaskDailyTrigger" -and $trigger.DaysInterval -eq 1 ) {
@@ -72,7 +78,7 @@ if ($GPOManagementDetected) {
7278 $configChanged = $true
7379 }
7480
75- # Check if delay has changed
81+ # === Check if delay has changed ===
7682 $randomDelay = [TimeSpan ]::ParseExact($WAUConfig.WAU_UpdatesTimeDelay , " hh\:mm" , $null )
7783 $timeTrigger = $currentTriggers | Where-Object { $_.CimClass.CimClassName -ne " MSFT_TaskLogonTrigger" } | Select-Object - First 1
7884 if ($timeTrigger.RandomDelay -match ' ^PT(?:(\d+)H)?(?:(\d+)M)?$' ) {
@@ -84,7 +90,7 @@ if ($GPOManagementDetected) {
8490 $configChanged = $true
8591 }
8692
87- # Check if schedule time has changed
93+ # === Check if schedule time has changed ===
8894 if ($currentIntervalType -ne " None" -and $currentIntervalType -ne " Never" ) {
8995 if ($timeTrigger ) {
9096 $currentTime = [DateTime ]::Parse($timeTrigger.StartBoundary ).ToString(" HH:mm:ss" )
@@ -94,12 +100,16 @@ if ($GPOManagementDetected) {
94100 }
95101 }
96102
97- # Only update triggers if configuration has changed
103+ # === Update triggers if configuration changed ===
98104 if ($configChanged ) {
99105 $taskTriggers = @ ()
106+
107+ # Add logon trigger if enabled
100108 if ($WAUConfig.WAU_UpdatesAtLogon -eq 1 ) {
101109 $tasktriggers += New-ScheduledTaskTrigger - AtLogOn
102110 }
111+
112+ # Add time-based trigger based on interval type
103113 if ($WAUConfig.WAU_UpdatesInterval -eq " Daily" ) {
104114 $tasktriggers += New-ScheduledTaskTrigger - Daily - At $WAUConfig.WAU_UpdatesAtTime - RandomDelay $randomDelay
105115 }
@@ -116,20 +126,18 @@ if ($GPOManagementDetected) {
116126 $tasktriggers += New-ScheduledTaskTrigger - Weekly - At $WAUConfig.WAU_UpdatesAtTime - DaysOfWeek 2 - WeeksInterval 4 - RandomDelay $randomDelay
117127 }
118128
119- # If trigger(s) set
129+ # Apply new triggers or disable task
120130 if ($taskTriggers ) {
121- # Edit scheduled task
122131 Set-ScheduledTask - TaskPath $WAUTask.TaskPath - TaskName $WAUTask.TaskName - Trigger $taskTriggers | Out-Null
123132 }
124- # If not, remove trigger(s)
125133 else {
126- # Remove by setting past due date
134+ # Disable by setting a past due date
127135 $tasktriggers = New-ScheduledTaskTrigger - Once - At " 01/01/1970"
128136 Set-ScheduledTask - TaskPath $WAUTask.TaskPath - TaskName $WAUTask.TaskName - Trigger $tasktriggers | Out-Null
129137 }
130138 }
131139
132- # Log latest applied config
140+ # Log applied configuration
133141 Add-Content - Path $GPOLogFile - Value " `n Latest applied settings:"
134142 $WAUConfig.PSObject.Properties | Where-Object { $_.Name -like " WAU_*" } | Select-Object Name, Value | Out-File - Encoding default - FilePath $GPOLogFile - Append
135143
0 commit comments