From d987820bf8c790db1b98bceff3ccd8714e5344e6 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Mon, 2 Sep 2024 22:58:12 +0200 Subject: [PATCH] Review log creation --- Sources/Winget-AutoUpdate/Winget-Upgrade.ps1 | 3 +- .../functions/Invoke-LogRotation.ps1 | 64 ++++++------------- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/Sources/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Sources/Winget-AutoUpdate/Winget-Upgrade.ps1 index 4b50a8d..016e4f6 100644 --- a/Sources/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Sources/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -11,6 +11,7 @@ Get-ChildItem "$WorkingDir\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-O #Config console output encoding $null = cmd /c '' [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 +$Script:ProgressPreference = 'SilentlyContinue' #Log initialisation $LogFile = "$WorkingDir\logs\updates.log" @@ -24,7 +25,7 @@ $Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId if ($IsSystem) { #If log file doesn't exist, force create it if (!(Test-Path -Path $LogFile)) { - New-Item -Path $LogFile -ItemType File -Force | Out-Null + Write-ToLog "New log file created" } # Check if Intune Management Extension Logs folder exists if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue)) { diff --git a/Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 b/Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 index ebc4644..19309fe 100644 --- a/Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 +++ b/Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 @@ -1,55 +1,28 @@ #Function to rotate the logs function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) { - <# - .SYNOPSIS - Handle log rotation. - .DESCRIPTION - Invoke-LogRotation handles log rotation - .NOTES - Author: Øyvind Kallstad (Minimized and changed for WAU 12.01.2023 by Göran Axel Johannesson) - URL: https://www.powershellgallery.com/packages/Communary.Logger/1.1 - Date: 21.11.2014 - Version: 1.0 - #> - try { - # get current size of log file - $currentSize = (Get-Item $LogFile).Length + # if MaxLogFiles is 1 just keep the original one and let it grow + if (-not($MaxLogFiles -eq 1)) { - # get log name - $logFileName = Split-Path $LogFile -Leaf - $logFilePath = Split-Path $LogFile - $logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName) - $logFileNameExtension = [System.IO.Path]::GetExtension($logFileName) + try { + # get current size of log file + $currentSize = (Get-Item $LogFile).Length + + # get log name + $logFileName = Split-Path $LogFile -Leaf + $logFilePath = Split-Path $LogFile + $logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName) + $logFileNameExtension = [System.IO.Path]::GetExtension($logFileName) - # if MaxLogFiles is 1 just keep the original one and let it grow - if (-not($MaxLogFiles -eq 1)) { if ($currentSize -ge $MaxLogSize) { # construct name of archived log file $newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension + Rename-Item -Path $LogFile -NewName $newLogFileName -Force -Confirm:$false - # copy old log file to new using the archived name constructed above - Copy-Item -Path $LogFile -Destination (Join-Path (Split-Path $LogFile) $newLogFileName) - - # Create a new log file - try { - Remove-Item -Path $LogFile -Force - New-Item -ItemType File -Path $LogFile -Force - #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 - } - catch { - Return $False - } + # create new file + Write-ToLog "New log file created" # if MaxLogFiles is 0 don't delete any old archived log files if (-not($MaxLogFiles -eq 0)) { @@ -77,8 +50,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) { Return $True } } + + catch { + Return $False + } + } - catch { - Return $False - } + }