当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 管理 DNS 区域

作者:精品下载站 日期:2024-12-14 20:48:17 浏览:16 分类:玩电脑

如何使用 PowerShell 管理 DNS 区域


如果您发现自己使用 DNS MMC 管理单元对 Microsoft DNS 服务器进行更改,您可能会浪费大量时间。了解如何使用 PowerShell 添加 DNS 区域并完全管理它们!

为什么?因为可以使用 PowerShell 从 MMC 创建、修改或删除任何 DNS 对象!通过使用 PowerShell 管理 DNS,您不仅可以从命令行控制事物,还可以将这些命令放入脚本中以自动执行各种耗时的任务。

为了限制本文的范围,我们将只关注使用 PowerShell 管理 DNS 区域,尽管也完全可以管理其他 DNS 对象(例如记录和服务器本身)。

先决条件

在我们深入讨论之前,您需要了解一些先决条件。首先,我假设您有权使用 PowerShell 从 Windows 服务器读取、修改和删除 DNS 区域。

其次,我将演示来自具有 AD 集成区域的 Active Directory 域中的 DNS 服务器的一些概念。 PowerShell 仍然能够管理 Active Directory 之外的区域和记录,但结果可能与我在这里向您展示的结果不太一样。

最后,您需要确保您的客户端上安装了特定于您的操作系统的远程服务器管理工具 (RSAT) 版本。

相关:如何安装 Active Directory 模块并连接

验证 DNS 服务器模块

现在我们已经解决了这个问题,让我们首先确保 DNSServer 模块可供我们使用。为此,我将使用 Get-Module cmdlet。

PS> Get-Module DnsServer -ListAvailable
 
 
     Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules
 
 
 ModuleType Version    Name                                ExportedCommands
 ---------- -------    ----                                ----------------
 Manifest   2.0.0.0    DnsServer                           {Add-DnsServerConditionalForwarderZone, Add-DnsServerDirectoryPartition, Add-DnsServerForwarder, Add-DnsServerPrimaryZone...}
 
Great! It looks like our module is downloaded and we have some available commands. Let's now see what commands we have to work with DNS zones.
 
PS> Get-Command -Module DnsServer -Noun *Zone*
 
 CommandType     Name                                               Version    Source
 -----------     ----                                               -------    ------
 Function        Add-DnsServerConditionalForwarderZone              2.0.0.0    DnsServer
 Function        Add-DnsServerPrimaryZone                           2.0.0.0    DnsServer
 -- SNIP --

使用 PowerShell 添加 DNS 区域

首先,让我们使用 PowerShell 创建一个区域。为此,我们将使用 Add-DnsServerPrimaryZone函数。最简单的方法是使用两个参数。这些参数是 NameReplicationScope。但是,在我们的示例中,我还将使用 ComputerName 参数,因为我是在远程计算机上调用此命令。

PS> Add-DnsServerPrimaryZone -Name testzone.mylab.local -ComputerName DC -ReplicationScope Forest

在上面您可以看到我的域名是 mylab.local,我的区域名称是 testzone。我的 DNS 服务器是 DC,因此我为 ComputerName 参数指定该服务器,最后,由于该服务器位于我的域中,我还必须设置 ReplicationScope,因此我选择了在我的 Active Directory 林中的所有其他 DNS 服务器之间复制此区域。

验证 DNS 区域创建

接下来,为了验证该区域是否已创建,我可以使用 Get-DnsServerZone 命令。我可以使用 Name 参数,但为了向您显示我拥有的所有区域,我将告诉 Get-DnsServerZone 查找所有这些区域。

PS> Get-DnsServerZone -ComputerName DC
 
 ZoneName                            ZoneType        IsAutoCreated   IsDsIntegrated  IsReverseLookupZone  IsSigned
 --------                            --------        -------------   --------------  -------------------  --------
 _msdcs.mylab.local                  Primary         False           True            False                False
 0.in-addr.arpa                      Primary         True            False           True                 False
 127.in-addr.arpa                    Primary         True            False           True                 False
 255.in-addr.arpa                    Primary         True            False           True                 False
 mylab.local                         Primary         False           True            False                False
 testzone.mylab.local                Primary         False           True            False                False
 TrustAnchors                        Primary         False           True            False                F

使用 PowerShell 删除 DNS 区域

为了确保我们经历 DNS 区域的整个生命周期,我将删除它。

PS> Remove-DnsServerZone -Name testzone.mylab.local -ComputerName DC -Confirm

这还将删除该区域中的所有记录,并且服务器将不再托管该区域,是否继续?

[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
 PS>  Get-DnsServerZone -ComputerName DC
 
 ZoneName                            ZoneType        IsAutoCreated   IsDsIntegrated  IsReverseLookupZone  IsSigned
 --------                            --------        -------------   --------------  -------------------  --------
 _msdcs.mylab.local                  Primary         False           True            False                False
 0.in-addr.arpa                      Primary         True            False           True                 False
 127.in-addr.arpa                    Primary         True            False           True                 False
 255.in-addr.arpa                    Primary         True            False           True                 False
 mylab.local                         Primary         False           True            False                False
 TrustAnchors                        Primary         False           True            False      

现在您已经了解了 DNS 区域,为什么不从下一个逻辑点开始学习这个有关管理 DNS 记录的详细分步教程中的 DNS 记录。

概括

在 PowerShell 中管理 DNS 区域还有更多可能。我鼓励您仔细查看 Get-Command -Module DnsServer -Noun Zone 中可能的所有命令。此命令为您提供 DnsServer 模块内名称中包含“Zone”的所有命令的列表。您会发现命令名称是不言自明的,如果您需要进一步调查,请始终使用 Get-Help 查阅每个命令的帮助。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯