From 13ff4355066b0c662394bd1aac1ea3403c8e9b30 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Sat, 31 Dec 2022 16:25:19 +0100 Subject: [PATCH] Updates at Time now active standalone + fixes --- Policies/en-US/WAU.adml | 2 - Winget-AutoUpdate/functions/Get-Policies.ps1 | 44 ++++++++++++++++---- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Policies/en-US/WAU.adml b/Policies/en-US/WAU.adml index 3ab3b13..7830262 100644 --- a/Policies/en-US/WAU.adml +++ b/Policies/en-US/WAU.adml @@ -70,8 +70,6 @@ If this policy is disabled or not configured, the default is No. If this policy is enabled, you can configure the Sheduled Task Update time: From 01:00 to 24:00 (Military/24 Hour Time) -If set, it will not take effect until 'Updates Interval' also has been changed! - If this policy is not configured or disabled, Updates at Time: (06:00 AM). 01:00 AM 02:00 diff --git a/Winget-AutoUpdate/functions/Get-Policies.ps1 b/Winget-AutoUpdate/functions/Get-Policies.ps1 index e7a31a2..9996bfb 100644 --- a/Winget-AutoUpdate/functions/Get-Policies.ps1 +++ b/Winget-AutoUpdate/functions/Get-Policies.ps1 @@ -1,5 +1,6 @@ #Function to get Domain/Local Policies (GPO) + Function Get-Policies { #Get WAU Policies and set the Configurations Registry Accordingly $WAUPolicies = Get-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate" -ErrorAction SilentlyContinue @@ -82,12 +83,42 @@ Function Get-Policies { if ($null -ne $($WAUPolicies.WAU_UpdatesAtTime) -and ($($WAUPolicies.WAU_UpdatesAtTime) -ne $($WAUConfig.WAU_UpdatesAtTime))) { New-ItemProperty $regPath -Name WAU_UpdatesAtTime -Value $($WAUPolicies.WAU_UpdatesAtTime) -Force | Out-Null - $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" + $service = New-Object -ComObject Schedule.Service + $service.Connect($env:COMPUTERNAME) + $folder = $service.GetFolder('\') + $task = $folder.GetTask("Winget-AutoUpdate") + $definition = $task.Definition + for($triggerId=1; $triggerId -le $definition.Triggers.Count; $triggerId++){ + if(($definition.Triggers.Item($triggerId).Type -eq "2") -or ($definition.Triggers.Item($triggerId).Type -eq "3")){ + $PreStartBoundary = ($definition.Triggers.Item($triggerId).StartBoundary).Substring(0,11) + $PostStartBoundary = ($definition.Triggers.Item($triggerId).StartBoundary).Substring(19,6) + $Boundary = $PreStartBoundary + $($WAUPolicies.WAU_UpdatesAtTime) + $PostStartBoundary + $definition.Triggers.Item($triggerId).StartBoundary = $Boundary + break + $triggerId-=1 + } + } + $folder.RegisterTaskDefinition($task.Name, $definition, 4, $null, $null, $null) | Out-Null $ChangedSettings++ } elseif ($null -eq $($WAUPolicies.WAU_UpdatesAtTime) -and $($WAUConfig.WAU_UpdatesAtTime) -ne "06:00:00") { New-ItemProperty $regPath -Name WAU_UpdatesAtTime -Value "06:00:00" -Force | Out-Null - $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" + $service = New-Object -ComObject Schedule.Service + $service.Connect($env:COMPUTERNAME) + $folder = $service.GetFolder('\') + $task = $folder.GetTask("Winget-AutoUpdate") + $definition = $task.Definition + for($triggerId=1; $triggerId -le $definition.Triggers.Count; $triggerId++){ + if(($definition.Triggers.Item($triggerId).Type -eq "2") -or ($definition.Triggers.Item($triggerId).Type -eq "3")){ + $PreStartBoundary = ($definition.Triggers.Item($triggerId).StartBoundary).Substring(0,11) + $PostStartBoundary = ($definition.Triggers.Item($triggerId).StartBoundary).Substring(19,6) + $Boundary = $PreStartBoundary + "06:00:00" + $PostStartBoundary + $definition.Triggers.Item($triggerId).StartBoundary = $Boundary + break + $triggerId-=1 + } + } + $folder.RegisterTaskDefinition($task.Name, $definition, 4, $null, $null, $null) | Out-Null $ChangedSettings++ } @@ -203,7 +234,7 @@ Function Get-Policies { } } else { - Remove-ItemProperty $regPath -Name WAU_UpdatesAtLogon -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty $regPath -Name WAU_UpdatesAtLogon -Value $($WAUPolicies.WAU_UpdatesAtLogon) -PropertyType DWord -Force | Out-Null $service = New-Object -ComObject Schedule.Service $service.Connect($env:COMPUTERNAME) $folder = $service.GetFolder('\') @@ -297,12 +328,7 @@ Function Get-Policies { $ChangedSettings++ } - if ($ChangedSettings -gt 0) { - Write-Log "Changed settings: $ChangedSettings" "Yellow" - } - else { - Write-Log "Changed settings: None" "Yellow" - } + Write-Log "Changed settings: $ChangedSettings" "Yellow" #Get WAU Configurations after Policies change $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"