Dmitry Porotnikov / PowerShell: Gather DNS Server Logs from Windows

Created Wed, 13 Sep 2023 14:25:29 +0000 Modified Wed, 13 Sep 2023 14:25:29 +0000
107 Words

PowerShell: Gather DNS Server Logs from Windows

When maintaining a Windows DNS server, pulling the DNS logs periodically is essential for monitoring and troubleshooting. PowerShell offers a powerful yet simple method to automate this. Below is a script that not only fetches the DNS server logs but also gathers detailed DNS configurations using native commands.

Here’s how you can use PowerShell to gather DNS server logs from a Windows server:

$destDirectory = "C:\DNSLogs"

if (-not (Test-Path $destDirectory)) {
    New-Item -ItemType Directory -Path $destDirectory
}

Get-WinEvent -LogName 'DNS Server' | Export-Csv -Path "$destDirectory\DNSServerEvents.csv" -NoTypeInformation

dnscmd /info > "$destDirectory\DNSConfig.txt"

Write-Output "Diagnostic logs have been gathered and saved to $destDirectory"