diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index e4aa2dc..c1993bc 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 "Checking 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`n" "Green" } catch { - Write-host "-> MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red + Write-ToLog "-> MS Visual C++ 2015-2022 installation failed.`n" "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.`n" "Magenta" } } else { - Write-Host "-> Prerequisites checked. OK" -ForegroundColor Green + Write-ToLog "-> Prerequisites checked. OK`n" "Green" } } function Install-WinGet { - Write-Host "`nChecking if WinGet is installed/up to date" -ForegroundColor 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 @@ -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`n" "Green" } } function Install-WingetAutoUpdate { - Write-Host "`nInstalling WAU..." -ForegroundColor Yellow + Write-ToLog "Installing 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 "Checking 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 "Uninstalling 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!`n" "Green" Start-sleep 1 } else { - Write-host "$InstallLocation not found! Uninstallation failed!" -ForegroundColor Red + Write-ToLog "$InstallLocation not found! Uninstallation failed!`n" "Red" } } catch { - Write-host "Uninstallation failed! Run as admin ?" -ForegroundColor Red + Write-ToLog "Uninstallation failed! Run as admin ?`n" "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 "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 } } 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,6 +597,16 @@ if ("$env:PROCESSOR_ARCHITEW6432" -ne "ARM64") { } } +#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-Host "`n" Write-Host "`t 888 888 d8888 888 888" -ForegroundColor Magenta Write-Host "`t 888 o 888 d88888 888 888" -ForegroundColor Magenta @@ -615,18 +621,19 @@ Write-Host "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -Foreground Write-Host "`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 "End of process." "Cyan" Start-Sleep 3 diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index b40963a..5d0dbab 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -8,14 +8,19 @@ 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 -#Run log initialisation function -Start-Init +#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." } @@ -343,6 +348,13 @@ if (Test-Network) { } } +#Adds SymLink if Intune managed +$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 "$IntuneLogPath\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 89ebb80..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" @@ -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/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 db1116e..5ac4e61 100644 --- a/Winget-AutoUpdate/functions/Write-ToLog.ps1 +++ b/Winget-AutoUpdate/functions/Write-ToLog.ps1 @@ -1,9 +1,36 @@ #Write to Log Function -function Write-ToLog ($LogMsg, $LogColor = "White") { +function Write-ToLog { - #Get log - $Log = "$(Get-Date -UFormat "%T") - $LogMsg" + [CmdletBinding()] + param( + [Parameter()] [String] $LogMsg, + [Parameter()] [String] $LogColor = "White", + [Parameter()] [Switch] $IsHeader = $false + ) + + #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