-1

We have multiple DNS zones, and what to find static DNS records that no longer respond to pings and delete them.

I've written a PS script, but I'm having two problems.

1 - test-connection fails unless it uses the FQDN. I can't seem to find out how to put the FQDN of failed DNS records into a variable that works with test-connection.
2 - when I try to use test-connection with a large domain, it returns a Quota violation error.

Here's my code:


$zones = Get-Content "C:\Users\ddavidson.EMF\Documents\Test\zones.txt"
$OutFile = Write-Output "C:\Users\ddavidson.EMF\Documents\Test\logfile.txt"
$hostn = Get-dnsserverresourcerecord -ComputerName "DNSBox" -zonename "ourzone.com" -RRType A
$zone = Get-DnsServerZone -ComputerName "DNSBox"
$zones = $zone.zonename
$names = $hostn.hostname
$hostna = $names + "." + $zone"

foreach ($name in $names){
 if (Test-Connection -ComputerName $names -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$name, Up"
    #Write-Output "$name, Up" | Out-File $OutFile -Append
  }
  else{
    Write-Host "$name,Down"
    #Write-Output "$name" | Out-File $OutFile -Append
    Remove-DnsServerResourceRecord -zonename "ourzone.com" -ComputerName rklw303pv -name $names -Force
  }
}

For now, I've left out getting the zones from the server and just hard coded it.

I'm incredibly new to PS, and I've been searching for things to help me out, but haven't been able to find anything.

TIA to any and all.

LotPings
  • 1,015
  • 7
  • 12
Dave Davidson
  • 11
  • 1
  • 1

1 Answers1

0
foreach ($name in $names){
 if (Test-Connection -ComputerName $names -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$name, Up"
    #Write-Output "$name, Up" | Out-File $OutFile -Append
  }
  else{
    Write-Host "$name,Down"
    #Write-Output "$name" | Out-File $OutFile -Append
    Remove-DnsServerResourceRecord -zonename "ourzone.com" -ComputerName rklw303pv -name $names -Force
  }
}

In your test-connection you specify -ComputerName $nameS :

With that, test-connection will send a ping to each entry in $names. Moreover, you will ping all your domain for each member of it with the foreach ($name in $names). So if you have a big domain it can explain your problem of quota.

You can trie with :

if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue)

$name without the s

Sorcha
  • 1,325
  • 8
  • 11