auto remove old logs

This commit is contained in:
2021-05-27 18:07:02 -04:00
parent b1721544ea
commit 5626201d4d
+7 -1
View File
@@ -16,9 +16,15 @@ $RootDirectory, $ScriptsDirectory, $ToolsDirectory, $LogsDirectory | ForEach-Obj
}
## Setup the log file for messages generated when this script is run
$LogFile = '{0}\ManagementScript_{1}.log' -f $LogsDirectory, (Get-Date -Format "yyyyMMddHHmmssffff")
$LogFilePrefix = "ManagementScript"
$LogFilePostfix = Get-Date -Format "yyyyMMddHHmmssffff"
$LogFile = "${LogsDirectory}\${LogFilePrefix}_${LogFilePostfix}.log"
If ( !(Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null }
## Delete log files older than 30 days
$LogFileAllowedAge = (Get-Date).AddDays(-30)
Get-ChildItem -Path "${LogsDirectory}\${LogFilePrefix}_*.*" -Filter '*.log' | Where-Object {$_.LastWriteTime -lt $LogFileAllowedAge} | Remove-Item -Force -ErrorAction SilentlyContinue | Out-Null
## Log a message to the log file
Function LogMsg {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)