From 66793ca0d750bc8088e21b1d73a70949f26c909c Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Mon, 2 Sep 2024 09:50:01 +0200 Subject: [PATCH] Fix for logging --- Private/Set-LoggingCapabilities.ps1 | 34 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Private/Set-LoggingCapabilities.ps1 b/Private/Set-LoggingCapabilities.ps1 index b07b87c..d4b65fb 100644 --- a/Private/Set-LoggingCapabilities.ps1 +++ b/Private/Set-LoggingCapabilities.ps1 @@ -2,7 +2,7 @@ [CmdletBinding()] param( [parameter()][string] $LogPath, - [parameter(Mandatory)][string] $ScriptPath, + [parameter()][string] $ScriptPath, [parameter(Mandatory)][int] $LogMaximum, [switch] $ShowTime, [string] $TimeFormat @@ -20,22 +20,26 @@ $null = New-Item -Path $FolderPath -ItemType Directory -Force -WhatIf:$false } if ($LogMaximum -gt 0) { - $ScriptPathFolder = [io.path]::GetDirectoryName($ScriptPath) - if ($ScriptPathFolder -eq $FolderPath) { - Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but log files are in the same folder as the script. Cleanup disabled." -Color Yellow, White, DarkCyan, White - return - } - $CurrentLogs = Get-ChildItem -LiteralPath $FolderPath | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $LogMaximum - if ($CurrentLogs) { - Write-Color -Text '[i] ', "Logs directory has more than ", $LogMaximum, " log files. Cleanup required..." -Color Yellow, DarkCyan, Red, DarkCyan - foreach ($Log in $CurrentLogs) { - try { - Remove-Item -LiteralPath $Log.FullName -Confirm:$false -WhatIf:$false - Write-Color -Text '[+] ', "Deleted ", "$($Log.FullName)" -Color Yellow, White, Green - } catch { - Write-Color -Text '[-] ', "Couldn't delete log file $($Log.FullName). Error: ', "$($_.Exception.Message) -Color Yellow, White, Red + if ($ScriptPath) { + $ScriptPathFolder = [io.path]::GetDirectoryName($ScriptPath) + if ($ScriptPathFolder -eq $FolderPath) { + Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but log files are in the same folder as the script. Cleanup disabled." -Color Yellow, White, DarkCyan, White + return + } + $CurrentLogs = Get-ChildItem -LiteralPath $FolderPath | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $LogMaximum + if ($CurrentLogs) { + Write-Color -Text '[i] ', "Logs directory has more than ", $LogMaximum, " log files. Cleanup required..." -Color Yellow, DarkCyan, Red, DarkCyan + foreach ($Log in $CurrentLogs) { + try { + Remove-Item -LiteralPath $Log.FullName -Confirm:$false -WhatIf:$false + Write-Color -Text '[+] ', "Deleted ", "$($Log.FullName)" -Color Yellow, White, Green + } catch { + Write-Color -Text '[-] ', "Couldn't delete log file $($Log.FullName). Error: ', "$($_.Exception.Message) -Color Yellow, White, Red + } } } + } else { + Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but no script path detected. Most likely running interactively. Cleanup disabled." -Color Yellow, White, DarkCyan, White } } else { Write-Color -Text '[i] ', "LogMaximum is set to 0 (Unlimited). No log files will be deleted." -Color Yellow, DarkCyan