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] 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)) {