PowerShell script for DNS Inventory


Here goes the PowerShell script for DNS Inventory for AD Integrated DNS zones. This script needs to be altered to set variable for DC Name and once ran, it would fetch details from each zone and would save it in separate files.

Updated the code to provide option to dynamic selection of DNS Server. Thanks for input from @singhalyogi22

Note: Skipping SOA records etc to avoid complexity

function Show-Menu
{
    param (
        [string]$Title = 'My Menu'
    )
    Clear-Host
    Write-Host "================ $Title ================"
    
    Write-Host "1: Press '1' for automatically selecting current primary DNS of this machine."
    Write-Host "2: Press '2' for automatically selecting current logon server."
    Write-Host "3: Press '3' for manually typing DNS Server Name to work upon."
    Write-Host "Q: Press 'Q' to quit."
}
 
Show-Menu -Title "Choice for DNS Server"
$selection = Read-Host "Which DNS Server, you want to have inventory from "
switch ($selection)
{
	'1' {
			$CurerntSystemIP = (Resolve-DnsName $Env:Computername|?{$_.Type -eq "A"}).IPAddress
			$DNSServer = (Resolve-DNSName (gwmi -class win32_Networkadapterconfiguration |?{$_.IPAddress -like $CurerntSystemIP}).DNSServerSearchOrder[0]).NameHost
        } 
	 '2' {
			$DNSServer = $env:logonserver.split("\")[2]
         } 
	 '3' {
			$DNSServer = Read-Host "Provide the DNS Name "
         }     
	 'Q' {
			Exit
         }     
}
Write-Host "Collecting DNS Inventory from $($DNSServer) ..." -foregroundcolor GREEN

$Summary = @()
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
	$Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer |Select-Object HostName, RecordType,TimeStamp,TimetoLive, @{Name='PTR Record';Expression={$_.RecordData.PtrDomainName}},@{Name='IPv4Address';Expression={$_.RecordData.IPv4Address}},@{Name='IPv6Address';Expression={$_.RecordData.IPv6Address}},@{Name='CNAME Record';Expression={$_.RecordData.HostNameAlias}},@{Name='TXT Record';Expression={$_.RecordData.DescriptiveText}},@{Name='NS Record';Expression={$_.RecordData.NameServer}},@{Name='MX Record';Expression={$_.RecordData.MailExchange}} |export-csv -Notype "$env:userprofile\desktop\$DNSServer_$($Zone.ZoneName).csv"
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s