Created
November 15, 2021 03:57
-
-
Save thegooddoctorgonzo/02846880d900068ee01cae714b9d521c 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 DNS log files older than 7 days | |
| #Called by Scheduled Task Remove Old DNS Logsfrom DCs leeroyjenkins | |
| $Error.Clear() | |
| Start-Transcript -Path C:\Scripts\RemoveOldDNSLogs\RemoveOldDNsLogs.log -Append -Force | |
| $Header = (Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- Script: " + $MyInvocation.MyCommand.Source | |
| $NotificationLog = new-object system.collections.arraylist | |
| . \\SHAREPATH\PEOCIODATA\ServerTeam\Steve\scripts\Functions\Write-ToMasterLog.ps1 | |
| . \\SHAREPATH\PEOCIODATA\ServerTeam\Steve\scripts\Functions\Collect-Errors.ps1 | |
| . \\SHAREPATH\PEOCIODATA\ServerTeam\Steve\scripts\Functions\Get-CurrentLine.ps1 | |
| . \\SHAREPATH\PEOCIODATA\ServerTeam\Steve\scripts\Functions\Test-FileLock.ps1 | |
| #Get DCs | |
| $DCs = Get-ADComputer -SearchBase "OU=Domain Controllers,DC=Ad,DC=DOMAIN" -Filter * | |
| #list of log folders | |
| $folders = new-object system.collections.arraylist | |
| foreach($DC in $DCs) | |
| { | |
| $folders.Add($(Get-Item -Path "\\$($DC.Name)\c$\Windows\System32\dns")) | Out-Null | |
| } | |
| foreach($folder in $folders) | |
| { | |
| if($files = Get-ChildItem $folder -Depth 0 -Filter *.log | Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-7)}) | |
| { | |
| foreach($file in $files) | |
| { | |
| $file | Remove-Item -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -InformationAction SilentlyContinue | |
| if($Error[0].Exception -like "*System.IO.IOException*") | |
| { | |
| $error.Remove($Error[0]) | |
| } | |
| if($Error) | |
| { | |
| Collect-Errors -LogLines $Error -Label "Error deleting DNS log " -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog) | |
| } | |
| else | |
| { | |
| $NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- Deleted DNS log $($file.basename) on $((($file.FullName).TrimStart("\")).split("\")[0])") | Out-Null | |
| } | |
| } | |
| } | |
| } | |
| 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