Skip to content

Commit 21652c0

Browse files
authored
Merge pull request #1022 from Romanitho/clean-logs
Clean logs
2 parents 4ffd27c + ff31b4f commit 21652c0

File tree

2 files changed

+64
-64
lines changed

2 files changed

+64
-64
lines changed

Sources/Winget-AutoUpdate/Winget-Upgrade.ps1

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
#region LOAD FUNCTIONS
22
# Get the Working Dir
3-
[string]$Script:WorkingDir = $PSScriptRoot;
3+
[string]$Script:WorkingDir = $PSScriptRoot
44

55
# Get Functions
6-
Get-ChildItem -Path "$($Script:WorkingDir)\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-Object { . $_.FullName; }
6+
Get-ChildItem -Path "$($Script:WorkingDir)\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-Object { . $_.FullName }
77
#endregion LOAD FUNCTIONS
88

9-
<# MAIN #>
10-
9+
#region INITIALIZATION
1110
# Config console output encoding
1211
$null = & "$env:WINDIR\System32\cmd.exe" /c ""
13-
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
14-
$Script:ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue;
12+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
13+
$Script:ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
1514

1615
# Set GitHub Repo
17-
[string]$Script:GitHub_Repo = "Winget-AutoUpdate";
16+
[string]$Script:GitHub_Repo = "Winget-AutoUpdate"
1817

1918
# Log initialization
20-
[string]$LogFile = [System.IO.Path]::Combine($Script:WorkingDir, 'logs', 'updates.log');
19+
[string]$LogFile = [System.IO.Path]::Combine($Script:WorkingDir, 'logs', 'updates.log')
20+
#endregion INITIALIZATION
2121

22-
#region Checking execution context
22+
#region CONTEXT
2323
# Check if running account is system or interactive logon System(default) otherwise User
24-
[bool]$Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem;
24+
[bool]$Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem
2525

2626
# Check for current session ID (O = system without ServiceUI)
27-
[Int32]$Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId;
28-
#endregion
27+
[Int32]$Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
28+
#endregion CONTEXT
2929

30-
#region Execution context and logging
30+
#region EXECUTION CONTEXT AND LOGGING
3131
# Preparation to run in current context
3232
if ($true -eq $IsSystem) {
3333

3434
#If log file doesn't exist, force create it
3535
if (!(Test-Path -Path $LogFile)) {
36-
Write-ToLog "New log file created";
36+
Write-ToLog "New log file created"
3737
}
3838

3939
#Check if running with session ID 0
4040
if ($SessionID -eq 0) {
4141
#Check if ServiceUI exists
42-
[string]$fp3 = [System.IO.Path]::Combine($Script:WorkingDir, 'ServiceUI.exe');
43-
[bool]$ServiceUI = Test-Path $fp3 -PathType Leaf;
44-
if ($true -eq $ServiceUI) {
42+
[string]$ServiceUIexe = [System.IO.Path]::Combine($Script:WorkingDir, 'ServiceUI.exe')
43+
[bool]$IsServiceUI = Test-Path $ServiceUIexe -PathType Leaf
44+
if ($true -eq $IsServiceUI) {
4545
#Check if any connected user
46-
$explorerprocesses = @(Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue);
46+
$explorerprocesses = @(Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
4747
if ($explorerprocesses.Count -gt 0) {
48-
Write-ToLog "Rerun WAU in system context with ServiceUI";
48+
Write-ToLog "Rerun WAU in system context with ServiceUI"
4949
Start-Process `
50-
-FilePath $fp3 `
50+
-FilePath $ServiceUIexe `
5151
-ArgumentList "-process:explorer.exe $env:windir\System32\conhost.exe --headless powershell.exe -NoProfile -ExecutionPolicy Bypass -File winget-upgrade.ps1" `
52-
-WorkingDirectory $WorkingDir;
53-
Wait-Process "ServiceUI" -ErrorAction SilentlyContinue;
54-
Exit 0;
52+
-WorkingDirectory $WorkingDir `
53+
-Wait
54+
Exit 0
5555
}
5656
else {
5757
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader
@@ -68,49 +68,47 @@ if ($true -eq $IsSystem) {
6868
else {
6969
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (User context)" -IsHeader
7070
}
71+
#endregion EXECUTION CONTEXT AND LOGGING
7172

72-
#region Get settings and Domain/Local Policies (GPO) if activated.
73-
Write-ToLog "Reading WAUConfig";
74-
$Script:WAUConfig = Get-WAUConfig;
75-
#endregion Get settings and Domain/Local Policies (GPO) if activated.
76-
77-
# Default name of winget repository used within this script
78-
[string]$DefaultWingetRepoName = 'winget';
73+
#region CONFIG & POLICIES
74+
Write-ToLog "Reading WAUConfig"
75+
$Script:WAUConfig = Get-WAUConfig
76+
#endregion CONFIG & POLICIES
7977

80-
#region Winget Source Custom
78+
#region WINGET SOURCE
8179
# Defining a custom source even if not used below (failsafe suggested by github/sebneus mentioned in issues/823)
82-
[string]$Script:WingetSourceCustom = $DefaultWingetRepoName;
80+
[string]$Script:WingetSourceCustom = 'winget'
8381

8482
# Defining custom repository for winget tool
8583
if ($null -ne $Script:WAUConfig.WAU_WingetSourceCustom) {
86-
$Script:WingetSourceCustom = $Script:WAUConfig.WAU_WingetSourceCustom.Trim();
87-
Write-ToLog "Selecting winget repository named '$($Script:WingetSourceCustom)'";
84+
$Script:WingetSourceCustom = $Script:WAUConfig.WAU_WingetSourceCustom.Trim()
85+
Write-ToLog "Selecting winget repository named '$($Script:WingetSourceCustom)'"
8886
}
89-
#endregion Winget Source Custom
87+
#endregion WINGET SOURCE
9088

9189
#region Log running context
9290
if ($true -eq $IsSystem) {
9391

9492
# Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files.
9593
$MaxLogFiles = $WAUConfig.WAU_MaxLogFiles
9694
if ($null -eq $MaxLogFiles) {
97-
[int32]$MaxLogFiles = 3;
95+
[int32]$MaxLogFiles = 3
9896
}
9997
else {
100-
[int32]$MaxLogFiles = $MaxLogFiles;
98+
[int32]$MaxLogFiles = $MaxLogFiles
10199
}
102100

103101
# Maximum size of log file.
104-
$MaxLogSize = $WAUConfig.WAU_MaxLogSize;
102+
$MaxLogSize = $WAUConfig.WAU_MaxLogSize
105103
if (!$MaxLogSize) {
106-
[int64]$MaxLogSize = [int64]1MB; # in bytes, default is 1 MB = 1048576
104+
[int64]$MaxLogSize = [int64]1MB # in bytes, default is 1 MB = 1048576
107105
}
108106
else {
109-
[int64]$MaxLogSize = $MaxLogSize;
107+
[int64]$MaxLogSize = $MaxLogSize
110108
}
111109

112110
#LogRotation if System
113-
[bool]$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize;
111+
[bool]$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
114112
if ($false -eq $LogRotate) {
115113
Write-ToLog "An Exception occurred during Log Rotation..."
116114
}
@@ -119,60 +117,61 @@ if ($true -eq $IsSystem) {
119117

120118
#region Run Scope Machine function if run as System
121119
if ($true -eq $IsSystem) {
122-
Add-ScopeMachine;
120+
Add-ScopeMachine
123121
}
124122
#endregion Run Scope Machine function if run as System
125123

126124
#region Get Notif Locale function
127-
[string]$LocaleDisplayName = Get-NotifLocale;
128-
Write-ToLog "Notification Level: $($WAUConfig.WAU_NotificationLevel). Notification Language: $LocaleDisplayName" "Cyan";
129-
#endregion
125+
[string]$LocaleDisplayName = Get-NotifLocale
126+
Write-ToLog "Notification Level: $($WAUConfig.WAU_NotificationLevel). Notification Language: $LocaleDisplayName" "Cyan"
127+
#endregion Get Notif Locale function
130128

129+
#region MAIN
131130
#Check network connectivity
132131
if (Test-Network) {
133132

134133
#Check prerequisites
135134
if ($true -eq $IsSystem) {
136-
Install-Prerequisites;
135+
Install-Prerequisites
137136
}
138137

139138
#Check if Winget is installed and get Winget cmd
140-
[string]$Script:Winget = Get-WingetCmd;
141-
Write-ToLog "Selected winget instance: $($Script:Winget)";
139+
[string]$Script:Winget = Get-WingetCmd
140+
Write-ToLog "Selected winget instance: $($Script:Winget)"
142141

143142
if ($Script:Winget) {
144143

145144
if ($true -eq $IsSystem) {
146145

147146
#Get Current Version
148-
$WAUCurrentVersion = $WAUConfig.ProductVersion;
149-
Write-ToLog "WAU current version: $WAUCurrentVersion";
147+
$WAUCurrentVersion = $WAUConfig.ProductVersion
148+
Write-ToLog "WAU current version: $WAUCurrentVersion"
150149

151150
#Check if WAU update feature is enabled or not if run as System
152-
$WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate;
151+
$WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate
153152
#If yes then check WAU update if run as System
154153
if ($WAUDisableAutoUpdate -eq 1) {
155-
Write-ToLog "WAU AutoUpdate is Disabled." "Gray";
154+
Write-ToLog "WAU AutoUpdate is Disabled." "Gray"
156155
}
157156
else {
158-
Write-ToLog "WAU AutoUpdate is Enabled." "Green";
157+
Write-ToLog "WAU AutoUpdate is Enabled." "Green"
159158
#Get Available Version
160-
$Script:WAUAvailableVersion = Get-WAUAvailableVersion;
159+
$Script:WAUAvailableVersion = Get-WAUAvailableVersion
161160
#Compare
162161
if ((Compare-SemVer -Version1 $WAUCurrentVersion -Version2 $WAUAvailableVersion) -lt 0) {
163162
#If new version is available, update it
164-
Write-ToLog "WAU Available version: $WAUAvailableVersion" "DarkYellow";
165-
Update-WAU;
163+
Write-ToLog "WAU Available version: $WAUAvailableVersion" "DarkYellow"
164+
Update-WAU
166165
}
167166
else {
168-
Write-ToLog "WAU is up to date." "Green";
167+
Write-ToLog "WAU is up to date." "Green"
169168
}
170169
}
171170

172171
#Delete previous list_/winget_error (if they exist) if run as System
173-
[string]$fp4 = [System.IO.Path]::Combine($Script:WorkingDir, 'logs', 'error.txt');
172+
[string]$fp4 = [System.IO.Path]::Combine($Script:WorkingDir, 'logs', 'error.txt')
174173
if (Test-Path $fp4) {
175-
Remove-Item $fp4 -Force;
174+
Remove-Item $fp4 -Force
176175
}
177176

178177
#Get External ListPath if run as System
@@ -272,7 +271,7 @@ if (Test-Network) {
272271

273272
#Get outdated Winget packages
274273
Write-ToLog "Checking application updates on Winget Repository named '$($Script:WingetSourceCustom)' .." "DarkYellow"
275-
$outdated = Get-WingetOutdatedApps -src $Script:WingetSourceCustom;
274+
$outdated = Get-WingetOutdatedApps -src $Script:WingetSourceCustom
276275

277276
#If something unusual happened or no update found
278277
if ($outdated -like "No update found.*") {
@@ -373,7 +372,7 @@ if (Test-Network) {
373372
Else {
374373
#Get Winget system apps to escape them before running user context
375374
Write-ToLog "User logged on, get a list of installed Winget apps in System context..."
376-
Get-WingetSystemApps -src $Script:WingetSourceCustom;
375+
Get-WingetSystemApps -src $Script:WingetSourceCustom
377376

378377
#Run user context scheduled task
379378
Write-ToLog "Starting WAU in User context..."
@@ -389,6 +388,7 @@ if (Test-Network) {
389388
Exit 1
390389
}
391390
}
391+
#endregion MAIN
392392

393393
#End
394394
Write-ToLog "End of process!" "Cyan"

Sources/Winget-AutoUpdate/functions/Install-Prerequisites.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ function Install-Prerequisites {
9191
$WinGetAvailableVersion = ((Invoke-WebRequest $WinGeturl -UseBasicParsing | ConvertFrom-Json)[0].tag_name).TrimStart("v")
9292
}
9393
catch {
94-
#If fail set version to the latest version as of 2025-03-14
95-
$WinGetAvailableVersion = "1.10.340"
94+
#If fail set version to the latest version as of 2025-08-12
95+
$WinGetAvailableVersion = "1.11.430"
9696
}
9797
try {
9898
#Get Admin Context Winget Location
@@ -139,7 +139,7 @@ function Install-Prerequisites {
139139
Remove-Item -Path $WingetInstaller -Force -ErrorAction SilentlyContinue
140140
}
141141
else {
142-
Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "Green"
142+
Write-ToLog "-> WinGet is up to date." "Green"
143143
}
144144
Write-ToLog "Prerequisites checked. OK" "Green"
145145

0 commit comments

Comments
 (0)