|
1 | 1 | <# |
2 | 2 | .SYNOPSIS |
3 | | -Handle user interaction from shortcuts and show a Toast notification |
| 3 | + Handles user-initiated WAU update checks via shortcut. |
4 | 4 |
|
5 | 5 | .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. |
7 | 10 |
|
8 | 11 | .EXAMPLE |
9 | | -.\user-run.ps1 |
| 12 | + .\User-Run.ps1 |
10 | 13 |
|
| 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. |
11 | 18 | #> |
12 | 19 |
|
| 20 | +# Check if WAU is currently running |
13 | 21 | 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 | + } |
17 | 25 | } |
18 | 26 |
|
19 | 27 | <# MAIN #> |
20 | 28 |
|
21 | | -#Get Working Dir |
| 29 | +# Set working directory |
22 | 30 | $Script:WorkingDir = $PSScriptRoot |
23 | 31 |
|
24 | | -#Load external functions |
| 32 | +# Load required functions |
25 | 33 | . $WorkingDir\functions\Get-NotifLocale.ps1 |
26 | 34 | . $WorkingDir\functions\Start-NotifTask.ps1 |
27 | 35 |
|
28 | | -#Get Toast Locale function |
| 36 | +# Load notification locale |
29 | 37 | Get-NotifLocale |
30 | 38 |
|
31 | | -#Set common variables |
| 39 | +# Set common notification parameters |
32 | 40 | $OnClickAction = "$WorkingDir\logs\updates.log" |
33 | 41 | $Button1Text = $NotifLocale.local.outputs.output[11].message |
34 | 42 |
|
35 | 43 | 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 | + } |
53 | 51 |
|
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 |
67 | 78 | } |
68 | 79 | 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 |
73 | 84 | } |
0 commit comments