ps/Modules/Alkami.PowerShell.Common/Public/Remove-ORBLogFiles.ps1

82 lines
2.8 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Remove-ORBLogFiles {
<#
.SYNOPSIS
Deletes current ORB Logs in a folder
.DESCRIPTION
In a given directory path, this will delete either all log files or all rolled log files.
.PARAMETER LogDirectory
String representation of a path to a folder. Any log files in this folder could be deleted.
.PARAMETER SkipActiveLogs
Switch to ignore any currently active logs (ones that have not rolled)
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[Alias("LogPath")]
[string]$LogDirectory,
[Parameter(Mandatory = $false)]
[Alias("SkipActive")]
[switch]$SkipActiveLogs
)
$logLead = (Get-LogLeadName)
if ([string]::IsNullOrEmpty($LogDirectory)) {
$LogDirectory = (Get-OrbLogsPath)
}
$filter = if ($SkipActiveLogs) { "*.log.*" } else { "*.log*" };
# Remove any potential trailing wildcarded directories
while ($LogDirectory.EndsWith("\*") ) {
Write-Host "$logLead : Found a wildcard. Trimming..."
$LogDirectory = $LogDirectory.Substring(0, $LogDirectory.Length - 2)
}
# Tack on a single \* so that GCI plays nicely with both the Include and Exclude parameters
$LogDirectory = $LogDirectory + "\*"
# Force the array in case Get-ChildItem only returns one item
$logFiles = @()
$logFiles = [array](Get-ChildItem -Path $LogDirectory -Include $filter -File).Where({$_.Name -notmatch "\d{12}"})
# Slog files get saved with the date stamp in the filename
# For example: Alkami.Services.BillPayOrchestration-20210901.slog
# Super annoying
# Slog files also have file locks unless they've been written to, even if the date has rolled
# This should handle most cases
if ($SkipActiveLogs) {
$slogDateFormat = (Get-Date).ToString("yyyyMMdd")
$logFiles += [array](Get-ChildItem $logDirectory -Include "*.slog*" -Exclude "*$slogDateFormat.slog*" -File) -notmatch "\.(gz|7z|zip)"
} else {
$logFiles += [array](Get-ChildItem $logDirectory -Include "*.slog*" -File) -notmatch "\.(gz|7z|zip)"
}
if(Test-IsCollectionNullOrEmpty $logFiles) {
Write-Warning "$logLead : No log files to remove. Returning."
return;
}
Write-Host ("$logLead : Delete {1} {0} files" -f $filter, $logFiles.Count)
$errorCount = 0
foreach ($logFile in $logFiles) {
try {
if ((Test-Path -Path $logFile) -eq $false) {
Write-Warning "$loglead : File does not exist: $logFile"
continue
}
Write-Verbose ("$logLead : Removing log file {0}" -f $logFile.FullName)
Remove-Item -Path $logFile.FullName -Force
} catch {
Write-Warning $_.Exception
$errorCount++
}
}
Write-Host ("$logLead : {0} of {1} Deletes Complete" -f ($logFiles.Count - $errorCount), $logFiles.Count)
}