none
Need help with DNS record clean up using PowerShell commands RRS feed

  • Question

  • Hello! 

    I'm trying to figure out how to delete a list of NS records from all zones in DNS. I have around 90 zones to work with, so I'd like to script it rather than do it manually. The problem is, I can't seem to work out a way to have each DNS record on the list get deleted out of every DNS zone out there. Is there possibly a way to have each item on a CSV column run against every item on another column?

    For example in this CSV I want garbage1 and garbage2 to be deleted out of both test1.com and test2.com:

    zone,name

    test1.com,garbage1.test1.com

    test2.com,garbage2.test1.com

    In PowerShell this is what I've tried, but it processes each CSV column at the same time, so if you see my example CSV above, it would delete only garbage1.test1.com from test1.com and only garbage2.test1.com from test2.com.

    import-csv .\csvtest.csv |foreach {
    
    $zone = Get-DnsServerResourceRecord -zonename $_.zone -rrtype NS -computername dc01
    
    ForEach-Object {Remove-DnsServerResourceRecord -ZoneName $_.zone -rrtype NS -name "@" -recorddata $_.name -computername dc01}
    }

    And without PowerShell, I've come up with this, but only with a single zone at a time.

    for /f %a in (entries.txt) do dnscmd /recorddelete %a @ NS zone.com /f

    Any help would be really appreciated! Maybe there's a better approach then what I'm trying to do.

    Thanks,

    Thomas




    Tuesday, December 29, 2015 8:08 PM

Answers

  • For example in this CSV I want garbage1 and garbage2 to be deleted out of both test1.com and test2.com:

    zone,name

    test1.com,garbage1.test1.com

    test2.com,garbage2.test1.com

    In PowerShell this is what I've tried, but it processes each CSV column at the same time, so if you see my example CSV above, it would delete only garbage1.test1.com from test1.com and only garbage2.test1.com from test2.com.

    This would only delete those entries because the spreadsheet is only listing each entry once - if you want to delete multiple entries for the same host as you have it laid out here you would need to put two entries for each such as:

    test1.com, garbage1.test1.com

    test1.com, garbage2.test2.com

    test2.com, garbage1.test1.com

    test2.com, garbage2.test2.com

    Otherwise you could go about it by making two separate collections:

    $ThingsIWantToRemove = @()
    $ThingsIwantToRemove += "garbage1.test1.com"
    $ThingsIwantToRemove += "garbage2.test2.com"
    
    $CSV = import-csv -path c:\temp\mycsv.csv
    
    foreach ($a in $CSV)
    {
        DNS Zone info here
    
        foreach ($b in $ThingsIwantToRemove)
        {
            Remove Logic Here
        }
    }


    Wednesday, December 30, 2015 2:16 PM

All replies

  • For example in this CSV I want garbage1 and garbage2 to be deleted out of both test1.com and test2.com:

    zone,name

    test1.com,garbage1.test1.com

    test2.com,garbage2.test1.com

    In PowerShell this is what I've tried, but it processes each CSV column at the same time, so if you see my example CSV above, it would delete only garbage1.test1.com from test1.com and only garbage2.test1.com from test2.com.

    This would only delete those entries because the spreadsheet is only listing each entry once - if you want to delete multiple entries for the same host as you have it laid out here you would need to put two entries for each such as:

    test1.com, garbage1.test1.com

    test1.com, garbage2.test2.com

    test2.com, garbage1.test1.com

    test2.com, garbage2.test2.com

    Otherwise you could go about it by making two separate collections:

    $ThingsIWantToRemove = @()
    $ThingsIwantToRemove += "garbage1.test1.com"
    $ThingsIwantToRemove += "garbage2.test2.com"
    
    $CSV = import-csv -path c:\temp\mycsv.csv
    
    foreach ($a in $CSV)
    {
        DNS Zone info here
    
        foreach ($b in $ThingsIwantToRemove)
        {
            Remove Logic Here
        }
    }


    Wednesday, December 30, 2015 2:16 PM
  • Thank you! I really appreciate your response. You have a good point. I like the two collections idea, but what if I have over 100 things that I need to remove? Is it possible to pull a separate list instead of listing them all out in the script like that?

    Thanks a lot Bobby,

    Thomas

    Friday, January 1, 2016 5:49 AM