如何以编程方式更新 DNS 记录

How to Update DNS Records Programatically

我正在尝试更新位于(我相信)服务器上以下路径的 DNS 记录:

ServerName -> Forward Lookup Zones -> domain.com -> test

其中 DNS 记录称为 test,类型为 Host(A)

我从 here 下载了 DNSShell 模块并尝试使用以下命令更改 DNS 记录(包含 IP 地址)的 'Data' 列:

Set-DNSRecord -Identity "test.domain.com"

但是我得到了这个错误:

Cannot validate argument on parameter 'Identity'. The argument "test.domain.com" does not match the "^\.\root\MicrosoftDNS:MicrosoftDNS_" pattern. Supply an argument that matches "^\.\root\MicrosoftDNS:MicrosoftDNS_" and try the command again.

所以我更新了Identity参数如下:

Set-DNSRecord -Identity "\Servername\root\MicrosoftDNS:MicrosoftDNS_"

但现在我看到了:

Set-DNSRecord : Specified argument was out of the range of the valid values. Parameter name: Path

当我尝试添加一个-Path时,它说没有这样的参数!有谁知道我需要在哪里添加 DNS 的 test.domain.com 部分来告诉命令要更新哪条记录?该模块的这一部分的文档不完整,我似乎找不到任何替代方案。

我可以调用 Get-DNSRecord 并查看我需要更新的记录,但是任何对 Set 的尝试都会被阻止,因为我不知道这些路径是如何构建的。

非常感谢任何帮助。

我设法通过使用 ye olde 经典 CMD 命令克服了这个问题:

dnscmd /RecordAdd domain.com recordname RecordType /Aging /OpenAcl A 192.168.0.0

这会添加一条新记录,即使存在另一条同名(但 IP 不同)的记录,它似乎也会这样做。

所以在我的例子中,我必须先使用 dnscmd 删除现有记录,然后再添加新记录(因为我实际上只想更新现有记录的 IP 地址)。

删除dns记录的命令:

dnscmd /recorddelete domain.com recordname

可以找到更多详细信息 here

您也可以为 Microsoft 的 DNS 服务器使用默认的 PowerShell 模块,示例如下所示:

Add-DnsServerResourceRecordA -Name "recordname" -IPv4Address "192.168.0.0" -ZoneName "domain.com" -AllowUpdateAny -AgeRecord 

如果您需要在此之前删除,类似这样的方法应该有效:

Remove-DnsServerResourceRecord -ZoneName "domain.com" -Name "recordname" -RRType "A" -Force