Created
November 15, 2021 03:57
-
-
Save thegooddoctorgonzo/fa81ac5e0263c8e1330980fbf530bbe3 to your computer and use it in GitHub Desktop.
:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Steve Landry | |
| #20190705 | |
| #Delete ProfileUnity user log files | |
| #If log folder contains logs from more than 2 dates, the oldest files are deleted. The 2 most recent log file dates are retained | |
| #Called by Scheduled Task Remove Old PU Log Files on leeroyjenkins | |
| $Error.Clear() | |
| Start-Transcript -Path C:\Scripts\RemoveOldPULogFiles\RemoveOldPULogFiles.log -Append -Force | |
| $Header = (Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- Script: " + $MyInvocation.MyCommand.Source | |
| $NotificationLog = new-object system.collections.arraylist | |
| . PATH\Functions\Write-ToMasterLog.ps1 | |
| . PATH\Functions\Collect-Errors.ps1 | |
| . PATH\Functions\Get-CurrentLine.ps1 | |
| #list of log folders | |
| $folders = Get-Item -Path \\SHAREPATH\ProfileUnity\*\Logs | |
| #$folders = Get-Item -Path "\\XXXXXX-prodcomp\ProfileUnity\nolanrg#\Logs" | |
| foreach($folder in $folders) | |
| { | |
| $dateList = New-Object System.Collections.ArrayList | |
| if($files = Get-ChildItem $folder -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue) | |
| { | |
| #collect list of log file dates | |
| foreach($file in $files) | |
| { | |
| if(!($dateList.Contains($(Get-Date -Date $file.CreationTime.ToShortDateString())))) | |
| { | |
| $dateList.Add($(Get-Date -Date $file.CreationTime.ToShortDateString())) | Out-Null | |
| } | |
| if($Error) | |
| { | |
| Collect-Errors -LogLines $Error -Label "Error collecting file $($file.FullName)" -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog) | |
| } | |
| } | |
| #if more than 2 dates in log files, delete files with the 3rd most recent date and anything older | |
| if($dateList.Count -gt 2) | |
| { | |
| $dateList = $dateList | Sort-Object -Descending | |
| $lastDate = Get-Date $dateList[2].Date.AddDays(-1) | |
| foreach($file in ($files | Where-Object {$_.CreationTime -le $lastDate})) | |
| { | |
| $file | Remove-Item -Confirm:$false | |
| if($Error) | |
| { | |
| Collect-Errors -LogLines $Error -Label "Error removing file $($file.FullName)" -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog) | |
| } | |
| <#else | |
| { | |
| $NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- File $($File.FullName) successfully deleted") | Out-Null | |
| }#> | |
| } | |
| } | |
| } | |
| if($dateList) | |
| { | |
| $dateList.Clear() | |
| } | |
| } | |
| Remove-Variable datelist | |
| Stop-Transcript | |
| Collect-Errors -LogLines $Error -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog) | |
| $NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- Script EXITING normally") | Out-Null | |
| Write-ToMasterLog -Entries $NotificationLog -Header $Header |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment