From b8808c6584aafea8dff9a4f3666917de6b919312 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:29:22 +0200 Subject: [PATCH 1/5] Better handle Logs --- Winget-AutoUpdate-Install.ps1 | 113 ++++++++++-------- Winget-AutoUpdate/Winget-Upgrade.ps1 | 9 +- .../functions/Invoke-PostUpdateActions.ps1 | 1 + Winget-AutoUpdate/functions/Start-Init.ps1 | 65 ---------- Winget-AutoUpdate/functions/Write-ToLog.ps1 | 26 +++- 5 files changed, 92 insertions(+), 122 deletions(-) delete mode 100644 Winget-AutoUpdate/functions/Start-Init.ps1 diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index e4aa2dc..7e1667a 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -119,16 +119,16 @@ param( <# FUNCTIONS #> #Include external Functions -. "$PSScriptRoot\Winget-AutoUpdate\functions\Start-Init.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Invoke-ModsProtect.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Update-WinGet.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Update-StoreApps.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Add-Shortcut.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Write-ToLog.ps1" function Install-Prerequisites { - Write-Host "`nChecking prerequisites..." -ForegroundColor Yellow + Write-ToLog "`nChecking prerequisites..." "Yellow" #Check if Visual C++ 2019 or 2022 installed $Visual2019 = "Microsoft Visual C++ 2015-2019 Redistributable*" @@ -163,33 +163,33 @@ function Install-Prerequisites { else { $OSArch = "x86" } - Write-host "-> Downloading VC_redist.$OSArch.exe..." + Write-ToLog "-> Downloading VC_redist.$OSArch.exe..." $SourceURL = "https://aka.ms/vs/17/release/VC_redist.$OSArch.exe" $Installer = $WingetUpdatePath + "\VC_redist.$OSArch.exe" $ProgressPreference = 'SilentlyContinue' Invoke-WebRequest $SourceURL -UseBasicParsing -OutFile (New-Item -Path $Installer -Force) - Write-host "-> Installing VC_redist.$OSArch.exe..." + Write-ToLog "-> Installing VC_redist.$OSArch.exe..." Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait Remove-Item $Installer -ErrorAction Ignore - Write-host "-> MS Visual C++ 2015-2022 installed successfully" -ForegroundColor Green + Write-ToLog "-> MS Visual C++ 2015-2022 installed successfully" "Green" } catch { - Write-host "-> MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red + Write-ToLog "-> MS Visual C++ 2015-2022 installation failed." "Red" Start-Sleep 3 } } else { - Write-host "-> MS Visual C++ 2015-2022 will not be installed." -ForegroundColor Magenta + Write-ToLog "-> MS Visual C++ 2015-2022 will not be installed." "Magenta" } } else { - Write-Host "-> Prerequisites checked. OK" -ForegroundColor Green + Write-ToLog "-> Prerequisites checked. OK" "Green" } } function Install-WinGet { - Write-Host "`nChecking if WinGet is installed/up to date" -ForegroundColor Yellow + Write-ToLog "`nChecking if WinGet is installed/up to date" "Yellow" #Check available WinGet version, if fail set version to the latest version as of 2023-10-08 $WinGetAvailableVersion = Get-WinGetAvailableVersion @@ -212,7 +212,7 @@ function Install-WinGet { #Check if the current available WinGet isn't a Pre-release and if it's newer than the installed if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { - Write-Host "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" -ForegroundColor Red + Write-ToLog "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" "Red" #Check if $WingetUpdatePath exist if (!(Test-Path $WingetUpdatePath)) { @@ -221,35 +221,35 @@ function Install-WinGet { #Downloading and Installing Dependencies in SYSTEM context if (!(Get-AppxPackage -Name 'Microsoft.UI.Xaml.2.7')) { - Write-Host "-> Downloading Microsoft.UI.Xaml.2.7..." + Write-ToLog "-> Downloading Microsoft.UI.Xaml.2.7..." $UiXamlUrl = "https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.0" $UiXamlZip = "$WingetUpdatePath\Microsoft.UI.XAML.2.7.zip" Invoke-RestMethod -Uri $UiXamlUrl -OutFile $UiXamlZip Expand-Archive -Path $UiXamlZip -DestinationPath "$WingetUpdatePath\extracted" -Force try { - Write-Host "-> Installing Microsoft.UI.Xaml.2.7..." + Write-ToLog "-> Installing Microsoft.UI.Xaml.2.7..." Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\extracted\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx" -SkipLicense | Out-Null - Write-host "-> Microsoft.UI.Xaml.2.7 installed successfully" -ForegroundColor Green + Write-ToLog "-> Microsoft.UI.Xaml.2.7 installed successfully" "Green" } catch { - Write-Host "-> Failed to intall Wicrosoft.UI.Xaml.2.7..." -ForegroundColor Red + Write-ToLog "-> Failed to intall Wicrosoft.UI.Xaml.2.7..." "Red" } Remove-Item -Path $UiXamlZip -Force Remove-Item -Path "$WingetUpdatePath\extracted" -Force -Recurse } if (!(Get-AppxPackage -Name 'Microsoft.VCLibs.140.00.UWPDesktop')) { - Write-Host "-> Downloading Microsoft.VCLibs.140.00.UWPDesktop..." + Write-ToLog "-> Downloading Microsoft.VCLibs.140.00.UWPDesktop..." $VCLibsUrl = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" $VCLibsFile = "$WingetUpdatePath\Microsoft.VCLibs.x64.14.00.Desktop.appx" Invoke-RestMethod -Uri $VCLibsUrl -OutFile $VCLibsFile try { - Write-Host "-> Installing Microsoft.VCLibs.140.00.UWPDesktop..." + Write-ToLog "-> Installing Microsoft.VCLibs.140.00.UWPDesktop..." Add-AppxProvisionedPackage -Online -PackagePath $VCLibsFile -SkipLicense | Out-Null - Write-host "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully" -ForegroundColor Green + Write-ToLog "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully" "Green" } catch { - Write-Host "-> Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." -ForegroundColor Red + Write-ToLog "-> Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." "Red" } Remove-Item -Path $VCLibsFile -Force } @@ -258,17 +258,17 @@ function Install-WinGet { } elseif ($WinGetAvailableVersion -match "-pre") { - Write-Host "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" -ForegroundColor Yellow + Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" "Yellow" Update-StoreApps } else { - Write-Host "-> WinGet is up to date: v$WinGetInstalledVersion" -ForegroundColor Green + Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "Green" } } function Install-WingetAutoUpdate { - Write-Host "`nInstalling WAU..." -ForegroundColor Yellow + Write-ToLog "`nInstalling WAU..." "Yellow" try { #Copy files to location (and clean old install) @@ -451,21 +451,17 @@ function Install-WingetAutoUpdate { New-ItemProperty $regPath -Name WAU_StartMenuShortcut -Value 1 -PropertyType DWord -Force | Out-Null } - - #Log file and symlink initialization - Start-Init - #Security check - Write-host "`nChecking Mods Directory:" -ForegroundColor Yellow + Write-ToLog "`nChecking Mods Directory:" "Yellow" $Protected = Invoke-ModsProtect "$WingetUpdatePath\mods" if ($Protected -eq $True) { - Write-Host "The mods directory is now secured!`n" -ForegroundColor Green + Write-ToLog "The mods directory is now secured!`n" "Green" } elseif ($Protected -eq $False) { - Write-Host "The mods directory was already secured!`n" -ForegroundColor Green + Write-ToLog "The mods directory was already secured!`n" "Green" } else { - Write-Host "Error: The mods directory couldn't be verified as secured!`n" -ForegroundColor Red + Write-ToLog "Error: The mods directory couldn't be verified as secured!`n" "Red" } #Create Shortcuts @@ -482,14 +478,14 @@ function Install-WingetAutoUpdate { Add-Shortcut "wscript.exe" "${env:Public}\Desktop\WAU - Check for updated Apps.lnk" "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\user-run.ps1`"`"" "${env:SystemRoot}\System32\shell32.dll,-16739" "Manual start of Winget-AutoUpdate (WAU)..." } - Write-host "WAU Installation succeeded!" -ForegroundColor Green + Write-ToLog "WAU Installation succeeded!" "Green" Start-sleep 1 #Run Winget ? Start-WingetAutoUpdate } catch { - Write-host "WAU Installation failed! Error $_ - Try running me with admin rights" -ForegroundColor Red + Write-ToLog "WAU Installation failed! Error $_ - Try running me with admin rights" "Red" Start-sleep 1 return $False } @@ -497,7 +493,7 @@ function Install-WingetAutoUpdate { function Uninstall-WingetAutoUpdate { - Write-Host "`nUninstalling WAU..." -ForegroundColor Yellow + Write-ToLog "`nUninstalling WAU..." "Yellow" try { #Get registry install location @@ -534,15 +530,15 @@ function Uninstall-WingetAutoUpdate { Remove-Item -Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk" -Force | Out-Null } - Write-host "Uninstallation succeeded!" -ForegroundColor Green + Write-ToLog "Uninstallation succeeded!" "Green" Start-sleep 1 } else { - Write-host "$InstallLocation not found! Uninstallation failed!" -ForegroundColor Red + Write-ToLog "$InstallLocation not found! Uninstallation failed!" "Red" } } catch { - Write-host "Uninstallation failed! Run as admin ?" -ForegroundColor Red + Write-ToLog "Uninstallation failed! Run as admin ?" "Red" Start-sleep 1 } } @@ -569,19 +565,19 @@ function Start-WingetAutoUpdate { } if ($RunWinget -eq 1) { try { - Write-host "`nRunning Winget-AutoUpdate..." -ForegroundColor Yellow + Write-ToLog "`nRunning Winget-AutoUpdate..." "Yellow" Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue while ((Get-ScheduledTask -TaskName "Winget-AutoUpdate").State -ne 'Ready') { Start-Sleep 1 } } catch { - Write-host "Failed to run Winget-AutoUpdate..." -ForegroundColor Red + Write-ToLog "Failed to run Winget-AutoUpdate..." "Red" } } } else { - Write-host "Skip running Winget-AutoUpdate" + Write-ToLog "Skip running Winget-AutoUpdate" } } @@ -601,32 +597,43 @@ if ("$env:PROCESSOR_ARCHITEW6432" -ne "ARM64") { } } -Write-Host "`n" -Write-Host "`t 888 888 d8888 888 888" -ForegroundColor Magenta -Write-Host "`t 888 o 888 d88888 888 888" -ForegroundColor Magenta -Write-Host "`t 888 d8b 888 d88P888 888 888" -ForegroundColor Magenta -Write-Host "`t 888 d888b 888 d88P 888 888 888" -ForegroundColor Magenta -Write-Host "`t 888d88888b888 d88P 888 888 888" -ForegroundColor Magenta -Write-Host "`t 88888P Y88888 d88P 888 888 888" -ForegroundColor Cyan -Write-Host "`t 8888P Y8888 d88P 888 888 888" -ForegroundColor Magenta -Write-Host "`t 888P Y888 d88P 888 Y8888888P`n" -ForegroundColor Magenta -Write-Host "`t Winget-AutoUpdate $WAUVersion`n" -ForegroundColor Cyan -Write-Host "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -ForegroundColor Magenta -Write-Host "`t________________________________________________________`n`n" +#Config console output encoding +$null = cmd /c '' #Tip for ISE +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 + +# Workaround for ARM64 (Access Denied / Win32 internal Server error) +$Script:ProgressPreference = 'SilentlyContinue' + +#Set install log file +$Script:LogFile = "$WingetUpdatePath\logs\WAU-Installer.log" + +Write-ToLog "`n" +Write-ToLog "`t 888 888 d8888 888 888" "Magenta" +Write-ToLog "`t 888 o 888 d88888 888 888" "Magenta" +Write-ToLog "`t 888 d8b 888 d88P888 888 888" "Magenta" +Write-ToLog "`t 888 d888b 888 d88P 888 888 888" "Magenta" +Write-ToLog "`t 888d88888b888 d88P 888 888 888" "Magenta" +Write-ToLog "`t 88888P Y88888 d88P 888 888 888" "Cyan" +Write-ToLog "`t 8888P Y8888 d88P 888 888 888" "Magenta" +Write-ToLog "`t 888P Y888 d88P 888 Y8888888P`n" "Magenta" +Write-ToLog "`t Winget-AutoUpdate $WAUVersion`n" "Cyan" +Write-ToLog "`t https://github.com/Romanitho/Winget-AutoUpdate`n" "Magenta" +Write-ToLog "`t________________________________________________________`n`n" if (!$Uninstall) { - Write-host "Installing WAU to $WingetUpdatePath\" + Write-ToLog "Installing WAU to $WingetUpdatePath\" Install-Prerequisites Install-WinGet Install-WingetAutoUpdate } else { - Write-Host "Uninstalling WAU..." + Write-ToLog "Uninstalling WAU..." Uninstall-WingetAutoUpdate } if (Test-Path "$WingetUpdatePath\Version.txt") { Remove-Item "$WingetUpdatePath\Version.txt" -Force } -Write-host "`nEnd of process." -ForegroundColor Cyan + +Write-ToLog "`nEnd of process." "Cyan" Start-Sleep 3 diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index b40963a..e17558d 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -12,7 +12,7 @@ Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName } $Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem #Run log initialisation function -Start-Init +Write-ToLog -LogMsg "CHECK FOR APP UPDATES" -IsHeader #Get settings and Domain/Local Policies (GPO) if activated. $WAUConfig = Get-WAUConfig @@ -343,6 +343,13 @@ if (Test-Network) { } } +#Adds SymLink if Intune managed +$IntuneLodPath = "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" +if ((Test-Path "$IntuneLodPath") -and !(Test-Path "$IntuneLodPath\WAU-updates.log")) { + Write-ToLog "`nCreating SymLink for log file (WAU-updates) in Intune Management Extension log folder" "Yellow" + New-Item -Path "$IntuneLodPath\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue | Out-Null +} + #End Write-ToLog "End of process!" "Cyan" Start-Sleep 3 diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 7651c0f..1477174 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -146,6 +146,7 @@ function Invoke-PostUpdateActions { #Remove old functions / files $FileNames = @( + "$WorkingDir\functions\Start-Init.ps1", "$WorkingDir\functions\Get-Policies.ps1", "$WorkingDir\functions\Get-WAUCurrentVersion.ps1", "$WorkingDir\functions\Get-WAUUpdateStatus.ps1", diff --git a/Winget-AutoUpdate/functions/Start-Init.ps1 b/Winget-AutoUpdate/functions/Start-Init.ps1 deleted file mode 100644 index b937ceb..0000000 --- a/Winget-AutoUpdate/functions/Start-Init.ps1 +++ /dev/null @@ -1,65 +0,0 @@ -# Initialisation - -function Start-Init { - - #Config console output encoding - [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 - - # Workaround for ARM64 (Access Denied / Win32 internal Server error) - $Script:ProgressPreference = 'SilentlyContinue' - - $caller = Get-ChildItem $MyInvocation.PSCommandPath | Select-Object -Expand Name - if ($caller -eq "Winget-Upgrade.ps1") { - #Log Header - $Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" - $Log | Write-host - #Logs initialisation - $Script:LogFile = "$WorkingDir\logs\updates.log" - } - elseif ($caller -eq "Winget-AutoUpdate-Install.ps1") { - $Script:LogFile = "$WingetUpdatePath\logs\updates.log" - } - - if (!(Test-Path $LogFile)) { - #Create file if doesn't exist - New-Item -ItemType File -Path $LogFile -Force | Out-Null - - #Set ACL for users on logfile - $NewAcl = Get-Acl -Path $LogFile - $identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11 - $fileSystemRights = "Modify" - $type = "Allow" - $fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type - $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList - $NewAcl.SetAccessRule($fileSystemAccessRule) - Set-Acl -Path $LogFile -AclObject $NewAcl - } - elseif ((Test-Path $LogFile) -and ($caller -eq "Winget-AutoUpdate-Install.ps1")) { - #Set ACL for users on logfile - $NewAcl = Get-Acl -Path $LogFile - $identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11 - $fileSystemRights = "Modify" - $type = "Allow" - $fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type - $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList - $NewAcl.SetAccessRule($fileSystemAccessRule) - Set-Acl -Path $LogFile -AclObject $NewAcl - } - - #Check if Intune Management Extension Logs folder and WAU-updates.log exists, make symlink - if ((Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs") -and !(Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log")) { - Write-host "`nCreating SymLink for log file (WAU-updates) in Intune Management Extension log folder" -ForegroundColor Yellow - New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue | Out-Null - } - #Check if Intune Management Extension Logs folder and WAU-install.log exists, make symlink - if ((Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs") -and (Test-Path "$WorkingDir\logs\install.log") -and !(Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log")) { - Write-host "`nCreating SymLink for log file (WAU-install) in Intune Management Extension log folder" -ForegroundColor Yellow - New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ItemType SymbolicLink -Value "$WorkingDir\logs\install.log" -Force -ErrorAction SilentlyContinue | Out-Null - } - - if ($caller -eq "Winget-Upgrade.ps1") { - #Log file - $Log | out-file -filepath $LogFile -Append - } - -} diff --git a/Winget-AutoUpdate/functions/Write-ToLog.ps1 b/Winget-AutoUpdate/functions/Write-ToLog.ps1 index db1116e..a7b580b 100644 --- a/Winget-AutoUpdate/functions/Write-ToLog.ps1 +++ b/Winget-AutoUpdate/functions/Write-ToLog.ps1 @@ -1,9 +1,29 @@ #Write to Log Function -function Write-ToLog ($LogMsg, $LogColor = "White") { +function Write-ToLog ($LogMsg, $LogColor = "White", $IsHeader) { - #Get log - $Log = "$(Get-Date -UFormat "%T") - $LogMsg" + #Create file if doesn't exist + if (!(Test-Path $LogFile)) { + New-Item -ItemType File -Path $LogFile -Force | Out-Null + + #Set ACL for users on logfile + $NewAcl = Get-Acl -Path $LogFile + $identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11 + $fileSystemRights = "Modify" + $type = "Allow" + $fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type + $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList + $NewAcl.SetAccessRule($fileSystemAccessRule) + Set-Acl -Path $LogFile -AclObject $NewAcl + } + + #If header requested + if ($IsHeader) { + $Log = "`n##################################################`n# $LogMsg - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" + } + else { + $Log = "$(Get-Date -UFormat "%T") - $LogMsg" + } #Echo log $Log | Write-host -ForegroundColor $LogColor From bfb3a50d8619c84089a84718c504cd490dbdebe4 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:09:44 +0200 Subject: [PATCH 2/5] Fix GPO --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 5 +++-- Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 | 4 ++-- Winget-AutoUpdate/functions/Update-WAU.ps1 | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index e17558d..f5f8de2 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -11,11 +11,12 @@ Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName } #Check if running account is system or interactive logon $Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem -#Run log initialisation function +#Log initialisation +$LogFile = "$WorkingDir\logs\updates.log" Write-ToLog -LogMsg "CHECK FOR APP UPDATES" -IsHeader #Get settings and Domain/Local Policies (GPO) if activated. -$WAUConfig = Get-WAUConfig +$Script:WAUConfig = Get-WAUConfig if ($($WAUPolicies.WAU_ActivateGPOManagement -eq 1)) { Write-ToLog "WAU Policies management Enabled." } diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 1477174..3a1a4f5 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -200,10 +200,10 @@ function Invoke-PostUpdateActions { ### End of post update actions ### #Reset WAU_UpdatePostActions Value - $WAUConfig | New-ItemProperty -Name WAU_PostUpdateActions -Value 0 -Force | Out-Null + New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" -Name "WAU_PostUpdateActions" -Value 0 -Force | Out-Null #Get updated WAU Config - $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" + $Script:WAUConfig = Get-WAUConfig #log Write-ToLog "Post Update actions finished" "green" diff --git a/Winget-AutoUpdate/functions/Update-WAU.ps1 b/Winget-AutoUpdate/functions/Update-WAU.ps1 index be415f4..035c29c 100644 --- a/Winget-AutoUpdate/functions/Update-WAU.ps1 +++ b/Winget-AutoUpdate/functions/Update-WAU.ps1 @@ -41,10 +41,10 @@ function Update-WAU { Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue #Set new version to registry - $WAUConfig | New-ItemProperty -Name DisplayVersion -Value $WAUAvailableVersion -Force | Out-Null + New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" -Name "DisplayVersion" -Value $WAUAvailableVersion -Force | Out-Null #Set Post Update actions to 1 - $WAUConfig | New-ItemProperty -Name WAU_PostUpdateActions -Value 1 -Force | Out-Null + New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" -Name "WAU_PostUpdateActions" -Value 1 -Force | Out-Null #Send success Notif Write-ToLog "WAU Update completed." "Green" From b1ada1991d709e512ef7aa0f18e3af6fedd1ce97 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:12:44 +0200 Subject: [PATCH 3/5] spelling --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index f5f8de2..d4da44f 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -345,10 +345,10 @@ if (Test-Network) { } #Adds SymLink if Intune managed -$IntuneLodPath = "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -if ((Test-Path "$IntuneLodPath") -and !(Test-Path "$IntuneLodPath\WAU-updates.log")) { +$IntuneLogPath = "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" +if ((Test-Path "$IntuneLogPath") -and !(Test-Path "$IntuneLogPath\WAU-updates.log")) { Write-ToLog "`nCreating SymLink for log file (WAU-updates) in Intune Management Extension log folder" "Yellow" - New-Item -Path "$IntuneLodPath\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue | Out-Null + New-Item -Path "$IntuneLogPath\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue | Out-Null } #End From b48e8f894e9db6e9a3140292d8c2acb8bf44c2d0 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:13:54 +0200 Subject: [PATCH 4/5] adjustments --- Winget-AutoUpdate-Install.ps1 | 40 ++++++++++++++-------------- Winget-AutoUpdate/Winget-Upgrade.ps1 | 4 +++ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 7e1667a..b689276 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -128,7 +128,7 @@ param( function Install-Prerequisites { - Write-ToLog "`nChecking prerequisites..." "Yellow" + Write-ToLog "Checking prerequisites..." "Yellow" #Check if Visual C++ 2019 or 2022 installed $Visual2019 = "Microsoft Visual C++ 2015-2019 Redistributable*" @@ -171,25 +171,25 @@ function Install-Prerequisites { Write-ToLog "-> Installing VC_redist.$OSArch.exe..." Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait Remove-Item $Installer -ErrorAction Ignore - Write-ToLog "-> MS Visual C++ 2015-2022 installed successfully" "Green" + Write-ToLog "-> MS Visual C++ 2015-2022 installed successfully`n" "Green" } catch { - Write-ToLog "-> MS Visual C++ 2015-2022 installation failed." "Red" + Write-ToLog "-> MS Visual C++ 2015-2022 installation failed.`n" "Red" Start-Sleep 3 } } else { - Write-ToLog "-> MS Visual C++ 2015-2022 will not be installed." "Magenta" + Write-ToLog "-> MS Visual C++ 2015-2022 will not be installed.`n" "Magenta" } } else { - Write-ToLog "-> Prerequisites checked. OK" "Green" + Write-ToLog "-> Prerequisites checked. OK`n" "Green" } } function Install-WinGet { - Write-ToLog "`nChecking if WinGet is installed/up to date" "Yellow" + Write-ToLog "Checking if WinGet is installed/up to date" "Yellow" #Check available WinGet version, if fail set version to the latest version as of 2023-10-08 $WinGetAvailableVersion = Get-WinGetAvailableVersion @@ -607,21 +607,21 @@ $Script:ProgressPreference = 'SilentlyContinue' #Set install log file $Script:LogFile = "$WingetUpdatePath\logs\WAU-Installer.log" -Write-ToLog "`n" -Write-ToLog "`t 888 888 d8888 888 888" "Magenta" -Write-ToLog "`t 888 o 888 d88888 888 888" "Magenta" -Write-ToLog "`t 888 d8b 888 d88P888 888 888" "Magenta" -Write-ToLog "`t 888 d888b 888 d88P 888 888 888" "Magenta" -Write-ToLog "`t 888d88888b888 d88P 888 888 888" "Magenta" -Write-ToLog "`t 88888P Y88888 d88P 888 888 888" "Cyan" -Write-ToLog "`t 8888P Y8888 d88P 888 888 888" "Magenta" -Write-ToLog "`t 888P Y888 d88P 888 Y8888888P`n" "Magenta" -Write-ToLog "`t Winget-AutoUpdate $WAUVersion`n" "Cyan" -Write-ToLog "`t https://github.com/Romanitho/Winget-AutoUpdate`n" "Magenta" -Write-ToLog "`t________________________________________________________`n`n" +Write-Host "`n" +Write-Host "`t 888 888 d8888 888 888" -ForegroundColor Magenta +Write-Host "`t 888 o 888 d88888 888 888" -ForegroundColor Magenta +Write-Host "`t 888 d8b 888 d88P888 888 888" -ForegroundColor Magenta +Write-Host "`t 888 d888b 888 d88P 888 888 888" -ForegroundColor Magenta +Write-Host "`t 888d88888b888 d88P 888 888 888" -ForegroundColor Magenta +Write-Host "`t 88888P Y88888 d88P 888 888 888" -ForegroundColor Cyan +Write-Host "`t 8888P Y8888 d88P 888 888 888" -ForegroundColor Magenta +Write-Host "`t 888P Y888 d88P 888 Y8888888P`n" -ForegroundColor Magenta +Write-Host "`t Winget-AutoUpdate $WAUVersion`n" -ForegroundColor Cyan +Write-Host "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -ForegroundColor Magenta +Write-Host "`t________________________________________________________`n`n" if (!$Uninstall) { - Write-ToLog "Installing WAU to $WingetUpdatePath\" + Write-ToLog "Installing WAU to $WingetUpdatePath\`n" Install-Prerequisites Install-WinGet Install-WingetAutoUpdate @@ -635,5 +635,5 @@ if (Test-Path "$WingetUpdatePath\Version.txt") { Remove-Item "$WingetUpdatePath\Version.txt" -Force } -Write-ToLog "`nEnd of process." "Cyan" +Write-ToLog "End of process." "Cyan" Start-Sleep 3 diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index d4da44f..5d0dbab 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -8,6 +8,10 @@ Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName } <# MAIN #> +#Config console output encoding +$null = cmd /c '' +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 + #Check if running account is system or interactive logon $Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem From 2a9cbcb8201ee2fb57bbf5303c26274ca1d0272f Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:35:36 +0200 Subject: [PATCH 5/5] more --- Winget-AutoUpdate-Install.ps1 | 28 +++++++------- .../functions/Invoke-PostUpdateActions.ps1 | 4 +- .../functions/Update-StoreApps.ps1 | 14 +++---- Winget-AutoUpdate/functions/Update-WinGet.ps1 | 37 ++++++------------- Winget-AutoUpdate/functions/Write-ToLog.ps1 | 9 ++++- 5 files changed, 40 insertions(+), 52 deletions(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index b689276..c1993bc 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -246,7 +246,7 @@ function Install-WinGet { try { Write-ToLog "-> Installing Microsoft.VCLibs.140.00.UWPDesktop..." Add-AppxProvisionedPackage -Online -PackagePath $VCLibsFile -SkipLicense | Out-Null - Write-ToLog "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully" "Green" + Write-ToLog "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully." "Green" } catch { Write-ToLog "-> Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." "Red" @@ -258,17 +258,17 @@ function Install-WinGet { } elseif ($WinGetAvailableVersion -match "-pre") { - Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" "Yellow" + Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release." "Yellow" Update-StoreApps } else { - Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "Green" + Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion`n" "Green" } } function Install-WingetAutoUpdate { - Write-ToLog "`nInstalling WAU..." "Yellow" + Write-ToLog "Installing WAU..." "Yellow" try { #Copy files to location (and clean old install) @@ -452,16 +452,16 @@ function Install-WingetAutoUpdate { } #Security check - Write-ToLog "`nChecking Mods Directory:" "Yellow" + Write-ToLog "Checking Mods Directory:" "Yellow" $Protected = Invoke-ModsProtect "$WingetUpdatePath\mods" if ($Protected -eq $True) { - Write-ToLog "The mods directory is now secured!`n" "Green" + Write-ToLog "-> The mods directory is now secured!`n" "Green" } elseif ($Protected -eq $False) { - Write-ToLog "The mods directory was already secured!`n" "Green" + Write-ToLog "-> The mods directory was already secured!`n" "Green" } else { - Write-ToLog "Error: The mods directory couldn't be verified as secured!`n" "Red" + Write-ToLog "-> Error: The mods directory couldn't be verified as secured!`n" "Red" } #Create Shortcuts @@ -493,7 +493,7 @@ function Install-WingetAutoUpdate { function Uninstall-WingetAutoUpdate { - Write-ToLog "`nUninstalling WAU..." "Yellow" + Write-ToLog "Uninstalling WAU..." "Yellow" try { #Get registry install location @@ -530,15 +530,15 @@ function Uninstall-WingetAutoUpdate { Remove-Item -Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk" -Force | Out-Null } - Write-ToLog "Uninstallation succeeded!" "Green" + Write-ToLog "Uninstallation succeeded!`n" "Green" Start-sleep 1 } else { - Write-ToLog "$InstallLocation not found! Uninstallation failed!" "Red" + Write-ToLog "$InstallLocation not found! Uninstallation failed!`n" "Red" } } catch { - Write-ToLog "Uninstallation failed! Run as admin ?" "Red" + Write-ToLog "Uninstallation failed! Run as admin ?`n" "Red" Start-sleep 1 } } @@ -565,7 +565,7 @@ function Start-WingetAutoUpdate { } if ($RunWinget -eq 1) { try { - Write-ToLog "`nRunning Winget-AutoUpdate..." "Yellow" + Write-ToLog "Running Winget-AutoUpdate..." "Yellow" Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue while ((Get-ScheduledTask -TaskName "Winget-AutoUpdate").State -ne 'Ready') { Start-Sleep 1 @@ -621,7 +621,7 @@ Write-Host "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -Foreground Write-Host "`t________________________________________________________`n`n" if (!$Uninstall) { - Write-ToLog "Installing WAU to $WingetUpdatePath\`n" + Write-ToLog "Installing WAU to $WingetUpdatePath\" Install-Prerequisites Install-WinGet Install-WingetAutoUpdate diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 3a1a4f5..89b7185 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -37,11 +37,11 @@ function Invoke-PostUpdateActions { #Check if the current available WinGet isn't a Pre-release and if it's newer than the installed if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { Write-ToLog "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" "red" - Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) $true + Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) } elseif ($WinGetAvailableVersion -match "-pre") { Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" "yellow" - Update-StoreApps $true + Update-StoreApps } else { Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "green" diff --git a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 index 50608cc..6b8b39c 100644 --- a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 +++ b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 @@ -1,16 +1,15 @@ #Function to force an upgrade of Store Apps -Function Update-StoreApps ($Log = $false) { +Function Update-StoreApps { $force_string = "-> Forcing an upgrade of Store Apps (this can take a minute)..." $fail_string = "-> ...something went wrong!" #If not WSB or Server, upgrade Microsoft Store Apps! if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") { - switch ($Log) { - $true {Write-ToLog $force_string "yellow"} - Default {Write-Host $force_string -ForegroundColor Yellow} - } + + Write-ToLog $force_string "yellow" + try { $namespaceName = "root\cimv2\mdm\dmmap" $className = "MDM_EnterpriseModernAppManagement_AppManagement01" @@ -19,10 +18,7 @@ Function Update-StoreApps ($Log = $false) { return $true } catch { - switch ($Log) { - $true {Write-ToLog $fail_string "red"} - Default {Write-Host $fail_string -ForegroundColor Red} - } + Write-ToLog $fail_string "red" return $false } } diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index 4bccbb2..2a59427 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -1,51 +1,36 @@ #Function to download and update WinGet -Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { +Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath) { - $download_string = "-> Downloading WinGet MSIXBundle for App Installer..." - $install_string = "-> Installing WinGet MSIXBundle for App Installer..." - $success_string = "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" - $reset_string = "-> WinGet sources reset." - $fail_string = "-> Failed to install WinGet MSIXBundle for App Installer..." + $download_string = "-> Downloading WinGet MSIXBundle for App Installer..." + $install_string = "-> Installing WinGet MSIXBundle for App Installer..." + $success_string = "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" + $reset_string = "-> WinGet sources reset." + $fail_string = "-> Failed to install WinGet MSIXBundle for App Installer..." #Download WinGet MSIXBundle - switch ($Log) { - $true {Write-ToLog $download_string} - Default {Write-Host $download_string} - } + Write-ToLog $download_string $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile($WinGetURL, "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") #Install WinGet MSIXBundle in SYSTEM context try { - switch ($Log) { - $true {Write-ToLog $install_string} - Default {Write-Host $install_string} - } + Write-ToLog $install_string Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null - switch ($Log) { - $true {Write-ToLog $success_string "green"} - Default {Write-host $success_string -ForegroundColor Green} - } + Write-ToLog $success_string "green" #Reset WinGet Sources $ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') } if ($ResolveWingetPath) { - switch ($Log) { - $true {Write-ToLog $reset_string "green"} - Default {Write-Host $reset_string -ForegroundColor Green} - } + Write-ToLog $reset_string "green" #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path & $WingetPath source reset --force } } catch { - switch ($Log) { - $true {Write-ToLog $fail_string "red"} - Default {Write-Host $fail_string -ForegroundColor Red} - } + Write-ToLog $fail_string "red" Update-StoreApps } diff --git a/Winget-AutoUpdate/functions/Write-ToLog.ps1 b/Winget-AutoUpdate/functions/Write-ToLog.ps1 index a7b580b..5ac4e61 100644 --- a/Winget-AutoUpdate/functions/Write-ToLog.ps1 +++ b/Winget-AutoUpdate/functions/Write-ToLog.ps1 @@ -1,6 +1,13 @@ #Write to Log Function -function Write-ToLog ($LogMsg, $LogColor = "White", $IsHeader) { +function Write-ToLog { + + [CmdletBinding()] + param( + [Parameter()] [String] $LogMsg, + [Parameter()] [String] $LogColor = "White", + [Parameter()] [Switch] $IsHeader = $false + ) #Create file if doesn't exist if (!(Test-Path $LogFile)) {