Home > Net >  PowerShell: Two A records are shown for each SRV record - when there should only be one
PowerShell: Two A records are shown for each SRV record - when there should only be one

Time:02-20

Have an odd situation where the PowerShell script below is showing two A records for each SRV record - when there should only be one A record per SRV record. Sample output:

SRV Records

If you're running Active Directory in your environment, simply run the PowerShell script below and you should get the same results. The output file will be on your Desktop. I've re-written this script multiple times and after many trials and errors, still getting the same results. Any idea why the A record is listed twice, and how to get it to show only once?

# Show Active Directory SRV records by Site
$logpath = "$ENV:UserProfile\Desktop\"
####################################################
#find date and convert to string
$date=((Get-Date).ToString('yyyy-MM-dd-HH-mm-ss'))
#defines the path where to find the log file of this script
$Logfile = $logpath $date ".log"
#convert date and time to string
$date=((Get-Date).ToString('yyyy-MM-dd-HH-mm-ss'))
#get addomain
$domain = ((Get-WmiObject Win32_ComputerSystem).Domain)
#get all sites
$sites = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites).Name)

Function Write-Log
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
  Write-Log "Domainname: $domain"
  Write-Log "-------------" 
foreach ($site in $sites){
  $srv = (Resolve-DnsName -Type SRV _ldap._tcp.$site._sites.$domain | ft Name,Type,NameTarget,Port | Out-String)
  Write-Log "Sitename: $site"
  Write-Log "-------------"
  Write-Log "$srv"
}

CodePudding user response:

There might be some differences between the two A-Records, but you're missing them due to this lines here.

$srv = (Resolve-DnsName -Type SRV _ldap._tcp.$site._sites.$domain |
    ft Name,Type,NameTarget,Port | Out-String)

The ft command here is an alias for Format-Table, and the default parameter is a list of which Columns to select. In that selection, you're only keeping these fields:

  • Name
  • Type
  • NameTarget
  • Port

Now, there might be a lot of other fields we're not seeing, and I expect one of them contains some unique different value. So I would suggest you run this to see how it's different. (Maybe this device has two network interfaces and they both have different IPs, and thus both get their own set of DNS settings back from DHCP)

Resolve-DnsName -Type SRV _ldap._tcp.$site._sites.$domain | format-list

That will show you all the fields, and you can figure out why you have two records.

CodePudding user response:

After some trial and error, this gives me exactly what I need. Change:

$srv = (Resolve-DnsName -Type SRV _ldap._tcp.$site._sites.$domain | ft Name,Type,NameTarget,Port | Out-String)

to:

$srv = (Resolve-DnsName -Type SRV _ldap._tcp.$site._sites.$domain | select-object -First 2 | ft Name,Type,NameTarget,Port | Out-String)

Explanantion: The use of select-object allows one to trim off rows from a result object array.

  • Related