Skip to content

Commit 1a48e83

Browse files
authored
Merge pull request #1076 from Romanitho/copilot
Add documentation and refactor WAU PowerShell scripts
2 parents 2a25a92 + e308811 commit 1a48e83

31 files changed

+1143
-878
lines changed
Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,84 @@
11
<#
22
.SYNOPSIS
3-
Handle user interaction from shortcuts and show a Toast notification
3+
Handles user-initiated WAU update checks via shortcut.
44
55
.DESCRIPTION
6-
Act on shortcut run
6+
Provides a user-facing interface to manually trigger WAU update checks.
7+
Displays toast notifications for status updates (starting, running,
8+
completed, or error). Waits for the scheduled task to complete and
9+
shows the result.
710
811
.EXAMPLE
9-
.\user-run.ps1
12+
.\User-Run.ps1
1013
14+
.NOTES
15+
Triggered by desktop shortcut or Start menu entry.
16+
Uses the Winget-AutoUpdate scheduled task.
17+
Shows error details from logs\error.txt if update check fails.
1118
#>
1219

20+
# Check if WAU is currently running
1321
function Test-WAUisRunning {
14-
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) {
15-
Return $True
16-
}
22+
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) {
23+
Return $True
24+
}
1725
}
1826

1927
<# MAIN #>
2028

21-
#Get Working Dir
29+
# Set working directory
2230
$Script:WorkingDir = $PSScriptRoot
2331

24-
#Load external functions
32+
# Load required functions
2533
. $WorkingDir\functions\Get-NotifLocale.ps1
2634
. $WorkingDir\functions\Start-NotifTask.ps1
2735

28-
#Get Toast Locale function
36+
# Load notification locale
2937
Get-NotifLocale
3038

31-
#Set common variables
39+
# Set common notification parameters
3240
$OnClickAction = "$WorkingDir\logs\updates.log"
3341
$Button1Text = $NotifLocale.local.outputs.output[11].message
3442

3543
try {
36-
#Check if WAU is currently running
37-
if (Test-WAUisRunning) {
38-
$Message = $NotifLocale.local.outputs.output[8].message
39-
$MessageType = "warning"
40-
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
41-
break
42-
}
43-
#Run scheduled task
44-
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
45-
#Starting check - Send notification
46-
$Message = $NotifLocale.local.outputs.output[6].message
47-
$MessageType = "info"
48-
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
49-
#Sleep until the task is done
50-
While (Test-WAUisRunning) {
51-
Start-Sleep 3
52-
}
44+
# Check if WAU is already running
45+
if (Test-WAUisRunning) {
46+
$Message = $NotifLocale.local.outputs.output[8].message
47+
$MessageType = "warning"
48+
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
49+
break
50+
}
5351

54-
#Test if there was a list_/winget_error
55-
if (Test-Path "$WorkingDir\logs\error.txt") {
56-
$MessageType = "error"
57-
$Critical = Get-Content "$WorkingDir\logs\error.txt" -Raw
58-
$Critical = $Critical.Trim()
59-
$Critical = $Critical.Substring(0, [Math]::Min($Critical.Length, 50))
60-
$Message = "Critical:`n$Critical..."
61-
}
62-
else {
63-
$MessageType = "success"
64-
$Message = $NotifLocale.local.outputs.output[9].message
65-
}
66-
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
52+
# Start the WAU scheduled task
53+
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
54+
55+
# Send "starting" notification
56+
$Message = $NotifLocale.local.outputs.output[6].message
57+
$MessageType = "info"
58+
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
59+
60+
# Wait for task completion
61+
While (Test-WAUisRunning) {
62+
Start-Sleep 3
63+
}
64+
65+
# Check for errors in the update process
66+
if (Test-Path "$WorkingDir\logs\error.txt") {
67+
$MessageType = "error"
68+
$Critical = Get-Content "$WorkingDir\logs\error.txt" -Raw
69+
$Critical = $Critical.Trim()
70+
$Critical = $Critical.Substring(0, [Math]::Min($Critical.Length, 50))
71+
$Message = "Critical:`n$Critical..."
72+
}
73+
else {
74+
$MessageType = "success"
75+
$Message = $NotifLocale.local.outputs.output[9].message
76+
}
77+
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
6778
}
6879
catch {
69-
#Check failed - Just send notification
70-
$Message = $NotifLocale.local.outputs.output[7].message
71-
$MessageType = "error"
72-
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
80+
# Handle task start failure
81+
$Message = $NotifLocale.local.outputs.output[7].message
82+
$MessageType = "error"
83+
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
7384
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1-
#Send Notify Script
1+
<#
2+
.SYNOPSIS
3+
Displays a toast notification to the logged-in user.
24
3-
#get xml notif config
5+
.DESCRIPTION
6+
Reads notification configuration from an XML file and displays
7+
a Windows toast notification using the ToastNotificationManager API.
8+
This script is called by a scheduled task when WAU runs in system context.
9+
10+
.NOTES
11+
Configuration file: config\notif.xml
12+
Launcher ID: Windows.SystemToast.WAU.Notification
13+
#>
14+
15+
# Get WAU installation path from registry
416
$WAUinstalledPath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Romanitho\Winget-AutoUpdate\" -Name InstallLocation
17+
18+
# Load notification XML configuration
519
[xml]$NotifConf = Get-Content "$WAUinstalledPath\config\notif.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
620
if (!($NotifConf)) {
721
break
822
}
923

10-
#Load Assemblies
24+
# Load Windows notification assemblies
1125
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
1226
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
1327

14-
#Prepare XML
28+
# Parse notification XML
1529
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
1630
$ToastXml.LoadXml($NotifConf.OuterXml)
1731

18-
#Specify Launcher App ID
32+
# Specify toast launcher ID
1933
$LauncherID = "Windows.SystemToast.WAU.Notification"
2034

21-
#Prepare and Create Toast
35+
# Create and display the notification
2236
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXML)
2337
$ToastMessage.Tag = $NotifConf.toast.tag
2438
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)

Sources/Winget-AutoUpdate/WAU-Policies.ps1

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
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

1523
if ($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 "`nLatest 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

Comments
 (0)