class HostCheck { [string] $HostName = "" [string] $Fqdn = "" [string] $CName = "" [string] $IpAddress = "" [bool] $Exists = $false [bool] $RegisteredInExternalDNS = $false; } function HostExistsInExternal ([HostCheck] $hostCheck, [string] $ExternalDNSServer) { try { $CnameChain = resolve-dnsname -name $hostCheck.Fqdn -DnsOnly -Type A -NoHostsFile -Server $ExternalDNSServer -ErrorAction Ignore foreach ($chainItem in $CnameChain) { if ($chainItem.NameHost -ieq $hostCheck.CName) { $hostCheck.RegisteredInExternalDNS = $true return $hostCheck } } } catch { $hostCheck.RegisteredInExternalDNS = $false } $hostCheck.RegisteredInExternalDNS = $false return $hostCheck } function GetIPAddress ([HostCheck] $hostCheck) { try { $hostAddresses = [System.Net.Dns]::GetHostAddresses($hostCheck.Fqdn) $hostCheck.IpAddress = $hostAddresses.IPAddressToString $hostCheck.Exists = $true } catch { try { $hostAddresses = [System.Net.Dns]::GetHostAddresses($hostCheck.HostName) $hostCheck.IpAddress = $hostAddresses.IPAddressToString $hostCheck.Exists = $true } catch { $hostCheck.IpAddress = "" $hostCheck.Exists = $false } } return $hostCheck } function Effectory-DNSVerify ( [string] $ZoneName = "effectory.com", [string] $DNSServer = "DC1.effectory.local", [string] $ExternalDNSServer = "8.8.8.8", [string] $Type = "CName") { Clear-DnsClientCache $DNSRecords = Get-DnsServerResourceRecord -ZoneName $ZoneName -computername $DNSServer -RRType $Type # HostName RecordType Type Timestamp TimeToLive RecordData # -------- ---------- ---- --------- ---------- ---------- # accept CNAME 5 0 01:00:00 effectorycorporate2-accept.azurewebsites.net. # accept-authorization CNAME 5 0 01:00:00 authorization-web-api-accept.azurewebsites.net. # accept-beta-project CNAME 5 0 01:00:00 webclient-project-accept.azurewebsites.net. # accept-cluster CNAME 5 0 01:00:00 test-cluster-effectory-accept.azurewebsites.net. # accept-customer CNAME 5 0 01:00:00 test-customer-effectory-accept.azurewebsites.net. # accept-dashboard CNAME 5 0 01:00:00 dashboard-effectory-accept.azurewebsites.net. [HostCheck[]]$Result = @() foreach ($DNSRecord in $DNSRecords) { [HostCheck] $hostCheck = [HostCheck]::new() $hostCheck.HostName = $DNSRecord.HostName [string] $fqdn = $DNSRecord.HostName if (-not ($fqdn.ToLowerInvariant().EndsWith($ZoneName.ToLowerInvariant()))) { $fqdn = "$($DNSRecord.HostName).$($ZoneName)" } $hostCheck.Fqdn = $fqdn [string] $cname = $DNSRecord.RecordData.HostNameAlias if ($cname.EndsWith(".")) { $cname = $cname.Remove($cname.Length - 1, 1) } $hostCheck.CName = $cname $hostCheck = HostExistsInExternal -host $hostCheck -ExternalDNSServer $ExternalDNSServer $hostCheck = GetIPAddress -host $hostCheck $Result += $hostCheck } return $Result } function Effectory-DNSVerify-Csv ( [string[]] $FileName, [string] $ExternalDNSServer = "8.8.8.8") { Clear-DnsClientCache $DNSRecords = import-csv $FileName -Header 'HostName', 'Cname', 'Fqdn' # HostName,Cname,Fqdn # accept-authorization,authorization-web-api-accept.azurewebsites.net,accept-authorization.effectory.com # accept-customer,test-customer-effectory-accept.azurewebsites.net,accept-customer.effectory.com # accept-ecestore,test-ecestore-accept.azurewebsites.net,accept-ecestore.effectory.com [HostCheck[]]$Result = @() foreach ($DNSRecord in $DNSRecords) { [HostCheck] $hostCheck = [HostCheck]::new() $hostCheck.HostName = $DNSRecord.HostName $hostCheck.CName = $DNSRecord.Cname $hostCheck.Fqdn = $DNSRecord.Fqdn $hostCheck = HostExistsInExternal -host $hostCheck -ExternalDNSServer $ExternalDNSServer $hostCheck = GetIPAddress -host $hostCheck $Result += $hostCheck } return $Result } function Effectory-DNSClean-Csv ( [string[]] $FileName, [string] $DNSServer = "DC1.effectory.local", [string] $Type = "CNAME") { Clear-DnsClientCache $DNSRecords = import-csv $FileName -Header 'HostName', 'ZoneName' # HostName,ZoneName # accountmanagertools,effectory.local # alex,effectory.local # webstage.beste-werkgevers,effectory.local foreach ($DNSRecord in $DNSRecords) { if (($DNSRecord.HostName -eq "HostName")) { continue; } Write-Host "Deleting $($DNSRecord.HostName) from $($DNSRecord.ZoneName)" try { Remove-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $DNSRecord.ZoneName -RRType $Type -Name $DNSRecord.HostName -Force } catch { } } } #Effectory-DNSVerify -ZoneName "effectory.com" -DNSServer "DC1.effectory.local" -ExternalDNSServer "8.8.8.8" | Export-Csv -Path .\DNS-effectory-com.csv #Effectory-DNSVerify -ZoneName "effectory.local" -DNSServer "DC1.effectory.local" -ExternalDNSServer "DC2.effectory.local" | Export-Csv -Path .\DNS-effectory-local.csv Effectory-DNSVerify-Csv -FileName .\Book1.csv -ExternalDNSServer "8.8.8.8" | Export-Csv -Path .\DNS-vip.csv # | Format-Table # | Export-Csv -Path .\DNS-xxxx.csv #Effectory-DNSClean-Csv -FileName .\deletedns.csv -DNSServer "DC1.effectory.local"