Updates at Time now active standalone + fixes

pull/257/head
KnifMelti 2022-12-31 16:25:19 +01:00
parent bc66d8f98e
commit 13ff435506
2 changed files with 35 additions and 11 deletions

View File

@ -70,8 +70,6 @@ If this policy is disabled or not configured, the default is No.</string>
<string id="UpdatesAtTime_Explain">If this policy is enabled, you can configure the Sheduled Task Update time: <string id="UpdatesAtTime_Explain">If this policy is enabled, you can configure the Sheduled Task Update time:
From 01:00 to 24:00 (Military/24 Hour 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).</string> If this policy is not configured or disabled, Updates at Time: (06:00 AM).</string>
<string id="UpdatesAtTime01">01:00 AM</string> <string id="UpdatesAtTime01">01:00 AM</string>
<string id="UpdatesAtTime02">02:00</string> <string id="UpdatesAtTime02">02:00</string>

View File

@ -1,5 +1,6 @@
#Function to get Domain/Local Policies (GPO) #Function to get Domain/Local Policies (GPO)
Function Get-Policies { Function Get-Policies {
#Get WAU Policies and set the Configurations Registry Accordingly #Get WAU Policies and set the Configurations Registry Accordingly
$WAUPolicies = Get-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate" -ErrorAction SilentlyContinue $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))) { 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 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++ $ChangedSettings++
} }
elseif ($null -eq $($WAUPolicies.WAU_UpdatesAtTime) -and $($WAUConfig.WAU_UpdatesAtTime) -ne "06:00:00") { 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 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++ $ChangedSettings++
} }
@ -203,7 +234,7 @@ Function Get-Policies {
} }
} }
else { 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 = New-Object -ComObject Schedule.Service
$service.Connect($env:COMPUTERNAME) $service.Connect($env:COMPUTERNAME)
$folder = $service.GetFolder('\') $folder = $service.GetFolder('\')
@ -297,12 +328,7 @@ Function Get-Policies {
$ChangedSettings++ $ChangedSettings++
} }
if ($ChangedSettings -gt 0) { Write-Log "Changed settings: $ChangedSettings" "Yellow"
Write-Log "Changed settings: $ChangedSettings" "Yellow"
}
else {
Write-Log "Changed settings: None" "Yellow"
}
#Get WAU Configurations after Policies change #Get WAU Configurations after Policies change
$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"