Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Delete a DNS Domain Programmatically?

Tags:

c#

.net

dns

wmi

I am building a C# web app to manage our DNS servers and am using the WMI Namespace for everything. The only thing I am having trouble with is deleting DNS Domains. Here is my code:

internal static bool DeleteDomainFromDns(string DnsServerName, string ContainerName, string Name)
    {
        try
        {
            string Query = "SELECT * FROM MicrosoftDNS_Domain WHERE DnsServerName = '" + DnsServerName + "' AND ContainerName = '" + ContainerName + "' AND Name = '" + Name + "'";
            ObjectQuery qry = new ObjectQuery(Query);
            DnsProvider dns = new DnsProvider();
            ManagementObjectSearcher s = new ManagementObjectSearcher(dns.Session, qry);
            ManagementObjectCollection col = s.Get();
            dns.Dispose();

            foreach (ManagementObject obj in col)
            {
                obj.Delete(); //Exception occurs here
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

The error I get is: ManagementException was caught "Generic Failure". I've read online where people are deleting domains by using the zone namespace but that only works if the domain you want to delete is a zone itself. I need to delete domains that are not zones. Can anyone help?

like image 293
mcass20 Avatar asked Oct 12 '10 16:10

mcass20


People also ask

How do I delete a DNS record in powershell?

The Remove-DnsServerResourceRecord cmdlet removes resource record objects from a Domain Name System (DNS) zone. You can either use the Get-DnsServerResourceRecord cmdlet to specify an object, or you can specify the RRtype, Name and RecordData of the resource record you want to remove.

How do I remove a DNS zone from Active Directory?

Delete one zone To delete a single DNS zone, perform the following steps: Select a domain name from the Choose Zones to Delete menu. Click Delete. Click Delete on the next interface to confirm that you wish to delete the selected zone.


1 Answers

I have not found a way to delete a domain using WMI and also checked into a Powershell snapin called DNSShell but it doesn't look like there is a command to delete the domain.

like image 153
RJ. Avatar answered Sep 28 '22 22:09

RJ.