如何以编程方式删除 DNS 域?
我正在构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有找到使用 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.
您可以尝试使用 中的脚本 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 parameterpDeleteRecords
instead of the firstpAddRecords
you will be able to delete any record in the DNS.