Error Validation for valid DNS Zone

Welcome Forums General PowerShell Q&A Error Validation for valid DNS Zone

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #163083

    Participant
    Topics: 115
    Replies: 244
    Points: 142
    Rank: Participant

    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

  • #163119

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,440
    Helping Hand
    Rank: Community Hero

    Which argument ? Can you post the error here ?

    • #163211

      Participant
      Topics: 115
      Replies: 244
      Points: 142
      Rank: Participant
      $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.

  • #163215

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,440
    Helping Hand
    Rank: Community Hero

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

    • #163223

      Participant
      Topics: 115
      Replies: 244
      Points: 142
      Rank: Participant

      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.

You must be logged in to reply to this topic.