如何以编程方式删除 DNS 域?

发布于 2024-09-26 19:58:14 字数 1051 浏览 5 评论 0原文

我正在构建一个 C# Web 应用程序来管理我们的 DNS 服务器,并使用 WMI 命名空间来处理所有事情。我唯一遇到的问题是删除 DNS 域。这是我的代码:

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;
        }
    }

我得到的错误是: ManagementException 被捕获“一般失败”。我在网上读到人们通过使用区域命名空间来删除​​域,但只有当您要删除的域本身就是区域时才有效。我需要删除不是区域的域。有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随波逐流 2024-10-03 19:58:14

我还没有找到使用 WMI 删除域的方法,并且还检查了名为 DNSShell 的 Powershell 管理单元,但看起来没有删除域的命令。

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.

乖乖公主 2024-10-03 19:58:14

您可以尝试使用 中的脚本 DnsResource.vbs 删除资源记录。它仅使用 DNS WMI 提供程序。因此,如果它适用于您的海豚,您可以在 C# 程序中执行相同的操作。

您还可以考虑使用 DnsModifyRecordsInSet。在 Windos SDK (C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\netds\dns\modifyrecords) 中,您可以找到使用 DnsModifyRecordsInSet 的 C++ 示例。它演示了如何在 DNS 中添加记录。如果您使用第二个参数 pDeleteRecords 而不是第一个 pAddRecords,您将能够删除 DNS 中的任何记录。

You can try with the script DnsResource.vbs from Delete a Resource Record. It uses only DNS WMI Provider. So if it will work for your porpoise you can do the same in your C# program.

You can also consider to use DnsModifyRecordsInSet. In Windos SDK (C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\netds\dns\modifyrecords) you can find an example in C++ which use DnsModifyRecordsInSet. It demonstrate how to add a record in the DNS. If you use the second parameter pDeleteRecords instead of the first pAddRecords you will be able to delete any record in the DNS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文