@@ -11,8 +11,12 @@ Get-ChildItem "$WorkingDir\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-O
1111# Config console output encoding
1212$null = cmd / c ' '
1313[Console ]::OutputEncoding = [System.Text.Encoding ]::UTF8
14+ $Script :ProgressPreference = ' SilentlyContinue'
1415
15- # Log initialisation
16+ # Set GitHub Repo
17+ $Script :GitHub_Repo = " Winget-AutoUpdate"
18+
19+ # Log initialization
1620$LogFile = " $WorkingDir \logs\updates.log"
1721
1822# Check if running account is system or interactive logon
@@ -22,6 +26,21 @@ $Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
2226
2327# Check if running as system
2428if ($IsSystem ) {
29+ # If log file doesn't exist, force create it
30+ if (! (Test-Path - Path $LogFile )) {
31+ Write-ToLog " New log file created"
32+ }
33+ # Check if Intune Management Extension Logs folder exists
34+ if ((Test-Path - Path " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs" - ErrorAction SilentlyContinue)) {
35+ # Check if symlink WAU-updates.log exists, make symlink (doesn't work under ServiceUI)
36+ if (! (Test-Path - Path " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" - ErrorAction SilentlyContinue)) {
37+ $symLink = New-Item - Path " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" - ItemType SymbolicLink - Value $LogFile - Force - ErrorAction SilentlyContinue
38+ }
39+ # Check if install.log and symlink WAU-install.log exists, make symlink (doesn't work under ServiceUI)
40+ if ((Test-Path - Path (' {0}\logs\install.log' -f $WorkingDir ) - ErrorAction SilentlyContinue) -and ! (Test-Path - Path " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs\WAU-install.log" - ErrorAction SilentlyContinue)) {
41+ $symLink = (New-Item - Path " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs\WAU-install.log" - ItemType SymbolicLink - Value (' {0}\logs\install.log' -f $WorkingDir ) - Force - Confirm:$False - ErrorAction SilentlyContinue)
42+ }
43+ }
2544 # Check if running with session ID 0
2645 if ($SessionID -eq 0 ) {
2746 # Check if ServiceUI exists
@@ -30,20 +49,33 @@ if ($IsSystem) {
3049 # Check if any connected user
3150 $explorerprocesses = @ (Get-CimInstance - Query " SELECT * FROM Win32_Process WHERE Name='explorer.exe'" - ErrorAction SilentlyContinue)
3251 if ($explorerprocesses.Count -gt 0 ) {
52+ if ($symLink ) {
53+ $null = (New-Item " $WorkingDir \logs\symlink.txt" - Value $symLink - Force)
54+ }
3355 # Rerun WAU in system context with ServiceUI
3456 & $WorkingDir \ServiceUI.exe - process:explorer.exe $env: windir \System32\wscript.exe \`" $WorkingDir \Invisible.vbs\`" \`" powershell.exe - NoProfile - ExecutionPolicy Bypass - File \`" \`" $WorkingDir \winget- upgrade.ps1\`" \`" \`"
3557 Exit 0
3658 }
3759 else {
3860 Write-ToLog - LogMsg " CHECK FOR APP UPDATES (System context)" - IsHeader
61+ if ($symLink ) {
62+ Write-ToLog " SymLink for log file created in Intune Management Extension log folder"
63+ }
3964 }
4065 }
4166 else {
4267 Write-ToLog - LogMsg " CHECK FOR APP UPDATES (System context - No ServiceUI)" - IsHeader
68+ if ($symLink ) {
69+ Write-ToLog " SymLink for log file created in Intune Management Extension log folder"
70+ }
4371 }
4472 }
4573 else {
4674 Write-ToLog - LogMsg " CHECK FOR APP UPDATES (System context - Connected user)" - IsHeader
75+ if (Test-Path " $WorkingDir \logs\symlink.txt" ) {
76+ Write-ToLog " SymLink for log file created in Intune Management Extension log folder"
77+ Remove-Item " $WorkingDir \logs\symlink.txt" - Force
78+ }
4779 }
4880}
4981else {
@@ -80,14 +112,14 @@ if ($IsSystem) {
80112 # LogRotation if System
81113 $LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
82114 if ($LogRotate -eq $False ) {
83- Write-ToLog " An Exception occured during Log Rotation..."
115+ Write-ToLog " An Exception occurred during Log Rotation..."
84116 }
85117
86118 # Run post update actions if necessary if run as System
87119 if (! ($WAUConfig.WAU_PostUpdateActions -eq 0 )) {
88120 Invoke-PostUpdateActions
89121 }
90- # Run Scope Machine funtion if run as System
122+ # Run Scope Machine function if run as System
91123 Add-ScopeMachine
92124}
93125
@@ -121,7 +153,7 @@ if (Test-Network) {
121153 # Get Available Version
122154 $Script :WAUAvailableVersion = Get-WAUAvailableVersion
123155 # Compare
124- if ([version ]$WAUAvailableVersion.Replace (" -" , " . " ) -ne [version ]$WAUCurrentVersion.Replace (" -" , " . " )) {
156+ if ([version ]$WAUAvailableVersion.replace (" -n " , " " ) -gt [version ]$WAUCurrentVersion.replace (" -n " , " " )) {
125157 # If new version is available, update it
126158 Write-ToLog " WAU Available version: $WAUAvailableVersion " " Yellow"
127159 Update-WAU
@@ -157,7 +189,13 @@ if (Test-Network) {
157189 $Script :ReachNoPath = $False
158190 }
159191 if ($NewList ) {
160- Write-ToLog " Newer List downloaded/copied to local path: $ ( $WAUConfig.InstallLocation.TrimEnd (" " , " \" )) " " Yellow"
192+ if ($AlwaysDownloaded ) {
193+ Write-ToLog " List downloaded/copied to local path: $ ( $WAUConfig.InstallLocation.TrimEnd (" " , " \" )) " " Yellow"
194+ }
195+ else {
196+ Write-ToLog " Newer List downloaded/copied to local path: $ ( $WAUConfig.InstallLocation.TrimEnd (" " , " \" )) " " Yellow"
197+ }
198+ $Script :AlwaysDownloaded = $False
161199 }
162200 else {
163201 if ($WAUConfig.WAU_UseWhiteList -and (Test-Path " $WorkingDir \included_apps.txt" )) {
@@ -239,17 +277,15 @@ if (Test-Network) {
239277 # Fix and count the array if GPO List as ERROR handling!
240278 if ($GPOList ) {
241279 if ($UseWhiteList ) {
242- $WhiteList = $toUpdate.GetUpperBound (0 )
243- if ($null -eq $WhiteList ) {
280+ if (-not $toUpdate ) {
244281 Write-ToLog " Critical: Whitelist doesn't exist in GPO, exiting..." " Red"
245282 New-Item " $WorkingDir \logs\error.txt" - Value " Whitelist doesn't exist in GPO" - Force
246283 Exit 1
247284 }
248285 foreach ($app in $toUpdate ) { Write-ToLog " Include app ${app} " }
249286 }
250287 else {
251- $BlackList = $toSkip.GetUpperBound (0 )
252- if ($null -eq $BlackList ) {
288+ if (-not $toSkip ) {
253289 Write-ToLog " Critical: Blacklist doesn't exist in GPO, exiting..." " Red"
254290 New-Item " $WorkingDir \logs\error.txt" - Value " Blacklist doesn't exist in GPO" - Force
255291 Exit 1
@@ -262,14 +298,12 @@ if (Test-Network) {
262298 Write-ToLog " Checking application updates on Winget Repository..." " yellow"
263299 $outdated = Get-WingetOutdatedApps
264300
265- # If something unusual happened
266- if ($outdated -like " An unusual *" ) {
301+ # If something unusual happened or no update found
302+ if ($outdated -like " No update found. *" ) {
267303 Write-ToLog " $outdated " " cyan"
268- $outdated = $False
269304 }
270-
271305 # Run only if $outdated is populated!
272- if ( $outdated ) {
306+ else {
273307 # Log list of app to update
274308 foreach ($app in $outdated ) {
275309 # List available updates
@@ -346,13 +380,6 @@ if (Test-Network) {
346380 # Check if user context is activated during system run
347381 if ($IsSystem ) {
348382
349- # Adds SymLink if Intune managed
350- $IntuneLogPath = " ${env: ProgramData} \Microsoft\IntuneManagementExtension\Logs"
351- if ((Test-Path " $IntuneLogPath " ) -and ! (Test-Path " $IntuneLogPath \WAU-updates.log" )) {
352- Write-ToLog " Creating SymLink for log file (WAU-updates) in Intune Management Extension log folder" " Yellow"
353- New-Item - Path " $IntuneLogPath \WAU-updates.log" - ItemType SymbolicLink - Value $LogFile - Force - ErrorAction SilentlyContinue | Out-Null
354- }
355-
356383 # Run WAU in user context if feature is activated
357384 if ($WAUConfig.WAU_UserContext -eq 1 ) {
358385
@@ -376,7 +403,7 @@ if (Test-Network) {
376403 Write-ToLog " No explorer process found / Nobody interactively logged on..."
377404 }
378405 Else {
379- # Get Winget system apps to excape them befor running user context
406+ # Get Winget system apps to escape them before running user context
380407 Write-ToLog " User logged on, get a list of installed Winget apps in System context..."
381408 Get-WingetSystemApps
382409
0 commit comments