DNS log rotation on Active Directory

Following on from my DNS logging, here is a bit of PowerShell to help you get log rotation working properly on Active Directory – Put this PS1 script into the task scheduler on the AD DNS servers to run every hour:-

$current_log = Get-ChildItem C:\DNS_Logs\queries*.log | Select-Object
Copy-Item $current_log C:\DNS_Logs\archive\
$now = [double]::Parse((Get-Date -Uformat %Y%m%d%H%M%S))
Set-DnsServerDiagnostics -computername localhost -EnableLoggingToFile $False
Set-DnsServerDiagnostics -computername localhost -EnableLoggingToFile $True -LogFilePath C:\DNS_Logs\queries-$now.log

Note: this assumes logs are kept in C:\DNS_Logs and that C:\DNS_Logs\archive exists as directories