Error Validation for valid DNS Zone

I have a list of DNS zones that I get from a customer but some don’t exist in our AD. I’d like to spit out to lists, Good and Bad, and send them back to him.

The headers of the .csv are:

DomainName,DMARCRecordName,CurrentDMARCRecord,NewDMARCRecord
The "DomainName" = the Zone name.

I have this code:

$AllDMARCs = Import-Csv .\DMARC.csv

foreach ($DMARC in $AllDMARCs) {
    Try {
    Write-Host "Validating list of Zones: $_.DomainName"
    $ZoneinAD = Get-DnsServerResourceRecord -ZoneName $_.DomainName -ComputerName DNSSERVER -ErrorAction stop
    $ZoneinAD
    $ZoneinAD | Out-File goodnames.txt -Append
    }

    Catch{
    Write-Host "No such Zone as $_.DomainName" -ForegroundColor Red
    $_.DomainName | Out-File badnames.txt -Append
    }
}

…but I get “The argument is null or empty”

How can I get these two text files created?

Thank you

Which argument ? Can you post the error here ?

$AllDMARCs = Import-Csv .\DMARC.csv

foreach ($DMARC in $AllDMARCs) {
    Try {
    Write-Host "Validating list of Zones: $_.DomainName"
    $ZoneinAD = Get-DnsServerResourceRecord -ZoneName $_.DomainName -ComputerName DNSSERVER -ErrorAction SilentlyContinue
    $ZoneinAD
    $ZoneinAD | Out-File goodnames.txt -Append
    }

    Catch{
    Write-Host "No such Zone as $DMARC" -ForegroundColor Red
    $DMARC | Out-File badnames.txt -Append
    }
}

I played with the code a bit and see no errors now but

  1. $ZoneinAD has no results (and thus, no goodnames.txt is created)
  2. my “badnames.txt” file has all values from all headers. In other words, I do indeed trap the Zones that don’t exist, but I only want the value for “DomainName”. I get all the csv headers’.

Thank you.

When you set -ErrorAction to SilentlyContinue, it ignore any error and doesn’t go to catch block. Use -ErrorAction Stop instead.

same results, for both issues:

DomainName   DMARCRecordName     CurrentDMARCRecord                                                         NewDMARCRecord                              
----------   ---------------     ------------------                                                         -------------- 
"values are here" 

for 2), I only want the “DomainName” header/value in badnames.txt. Not the 4 of them.