Snippet Name: Clean-SQLLogFolder
Tags: PowerShell
Created Date: 2020-06-09 15:29:04
Last Modified Date: 2020-06-09 15:29:04
— Clean-SQLLogFolder
Dependencies:
DBA Notes:
Sample Output:
^###################################################################################################
^## Purpose:
^###################################################################################################
Clear-Host
$Error.Clear()
^#region Prerequisites
^###################################################################################################
^## Prerequisites (Load Modules and installs)
^###################################################################################################
^#endregion
^#region Load References
^###################################################################################################
^##Load References
^###################################################################################################
^#endregion
^#region Local Functions
^###################################################################################################
^## Local Functions
^###################################################################################################
function die {Write-Error "Error: $($args[0])";exit 1}
function verifySuccess {if (!$?) { die "$($args[0])" }}
^#endregion
^#region Variables
^###################################################################################################
^## Variables
^###################################################################################################
^#region Automatic Logging - Start
$LogFolder = [System.IO.Path]::GetDirectoryName($psise.CurrentFile.FullPath)
$JustTheFileName = [System.IO.Path]::GetFileName($psise.CurrentFile.FullPath)
$LogFile = [System.IO.Path]::Combine($LogFolder,$JustTheFileName.Replace(".ps1","") +"_"+ (Get-Date).ToString("yyyy-MM-dd") + ".txt")
Start-Transcript -Path $LogFile
$StartTime = Get-Date
^#endregion
^#endregion
^#region Houskeeping
^###################################################################################################
^## Housekeeping
^###################################################################################################
^#endregion
^#region The Work
^###################################################################################################
^## The Work
^###################################################################################################
^# Define the base directory to search in
$baseDirectories = @("C:\Program Files\Microsoft SQL Server","C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\LOGS")
^##$$baseDirectories = @("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16")
$LogPatterns= @("log","Logs")
$FilterPattern = @('*.txt','*.xel','*.dmp','*.log','*.trc','FDLAUNCHERRORLOG*','SQLAGENT*','ERRORLOG*')
$ActuallyDelete = $true
^# Use Get-ChildItem to recursively find all folders named 'Logs'
^# Output the found folders
foreach($BaseDirectory in $baseDirectories)
{
foreach($LogPattern in $LogPatterns)
{
$logFolders = Get-ChildItem -Path $baseDirectory -Recurse -Directory -Filter $LogPattern
foreach ($PathToCheck in $logFolders )
{
Write-Host "Validating " $PathToCheck.FullName -ForegroundColor DarkGreen
foreach ($extension in $FilterPattern)
{
if([System.IO.Directory]::Exists($PathToCheck.FullName))
{
##Get-ChildItem -Path "\\STORMBASE\D$\MSSQL14.MSSQLSERVER\MSSQL\Log\" -Filter '*.txt'
Write-Host "$extension"(Get-ChildItem -Path $PathToCheck.FullName -Filter $extension).Length -ForegroundColor DarkYellow
if ($ActuallyDelete)
{
Get-ChildItem -Path $PathToCheck.FullName -Filter '*.txt' | Remove-Item -ErrorAction SilentlyContinue
}
else
{
Get-ChildItem -Path $PathToCheck.FullName -Filter '*.txt' | Remove-Item -ErrorAction SilentlyContinue -WhatIf
}
Write-Host " $extension"(Get-ChildItem -Path $PathToCheck.FullName -Filter $extension).Length "Remaining After Cleanup." -ForegroundColor DarkYellow
}
}
}
}
}
^###################################################################################################
^## End Of The Work
^###################################################################################################
^#region Automatic Logging - End
^##tail of all Work: review log
$EndTime = (Get-Date)
$ts = New-TimeSpan -Start $StartTime -End $EndTime
$MyElapsedTime ="Elapsed Time hh:mm:ss:: " + ($ts.Hours).ToString("00") + ':'+ ($ts.Minutes).ToString("00") + ':' + ($ts.Seconds).ToString("00")
Write-Host $MyElapsedTime -ForegroundColor Yellow
Stop-Transcript
Invoke-Item $LogFile
^#endregion
^#endregion
^#region TheOutput
^###################################################################################################
^## The Output
^###################################################################################################
^#endregion
verifySuccess $MyInvocation.MyCommand.Name + " Failed"