使用 PowerShell 管理 DNS(Domain Name System) 服务器,你可以执行多种操作,如添加、删除和修改 DNS(Domain Name System) 记录,以及管理 DNS(Domain Name System) 区域。以下是一些常用的 cmdlet 示例:

关于 IPv4、IPv6 和 DNS(Domain Name System)协议的标准和技术文档,主要来源如下:


1. IPv4(Internet Protocol Version 4)标准和技术文档


2. IPv6(Internet Protocol Version 6)标准和技术文档


3. DNS(Domain Name System)标准和技术文档


4. 其他技术资源

  • IETF 官方网站

    • 所有这些标准文档都可以通过IETF的官方网站获得,IETF是互联网标准的制定机构,负责发布和更新关于IPv4、IPv6和DNS的技术标准。
    • IETF 官方网站
  • Wireshark 官方文档

    • Wireshark是一个网络分析工具,广泛用于捕获和分析IP和DNS流量,帮助理解这些协议的实际工作机制。
    • Wireshark DNS 相关文档
  • Cisco 和 Microsoft 文档

    • Cisco 提供了关于网络配置、IPv4/IPv6和DNS配置的技术文档。
    • Microsoft 提供了有关DNS服务器配置、IPv6支持等方面的详细指南。
    • Cisco DNS 文档
    • Microsoft DNS 文档

通过以上资源,您可以深入了解IPv4、IPv6和DNS的标准、技术细节以及如何在实际应用中实施这些协议。


关于 DNS(Domain Name System) 的标准和技术文档,主要来源包括以下几项:

1. IETF RFC 标准

2. IETF(互联网工程任务组)官方网站

  • 所有关于DNS的最新标准和技术文档都可以在IETF的官网上找到,这些文档包括DNS的扩展和安全协议等内容。
  • IETF 网站

3. DNSSEC 相关文档

4. 其他技术资源

  • Microsoft DNS 文档: 如果你在Windows环境下使用DNS服务,Microsoft的官方文档提供了详细的配置步骤和技术说明。

  • Cisco DNS 文档: Cisco设备中也有与DNS配置和故障排除相关的技术文档,尤其在企业级网络中。

  • BIND(Berkeley Internet Name Domain): BIND是最常用的DNS服务器软件,官方文档提供了详细的配置和使用指南。

5. Wireshark 官方文档

  • Wireshark 提供了网络协议分析工具,适用于DNS流量的捕获和分析,帮助网络工程师诊断DNS问题。

通过这些标准文档和技术资源,你可以深入了解DNS的工作原理、配置方法以及DNSSEC等安全扩展的实现细节。

 

国内 IPv4 和 IPv6 DNS(Domain Name System) 地址的表格化展示:

类型 IP 地址 / DNS
IPv4 地址 119.29.29.29
  223.5.5.5
  223.6.6.6
  180.184.1.1
  180.184.2.2
  180.76.76.76
  114.114.114.114
  114.114.115.115
IPv6 地址 2402:4e00::
  2400:3200::1
  2400:3200:baba::1
  240c::6666
  240c::6644

这个表格列出了所有您提供的 IPv4 和 IPv6 地址信息。


Get-DnsClientCache 完整拆解解构

命令归属:PowerShell DnsClient Module
 
适用系统:Windows 10 / Windows 11 / Server 2016+
 
命令:Get-DnsClientCache
 
模块:DnsClient.psd1
 
底层载体:DNS Client 服务(Dnscache)

一、底层原理

1. 本质定位

Get-DnsClientCache 是 PowerShell 封装命令,读取本地 DNS Client 服务(Dnscache)维护的内存 DNS 解析缓存。
 
Windows DNS 缓存分为两层:
  1. 进程内缓存(WS2_32 小缓存,存活时间短,不对外暴露)
  2. 系统级共享缓存(Dnscache 服务内存缓存,本机所有进程共用,Get-DnsClientCache 读取这一层)
关键事实:
 
DNS 缓存默认不保存在磁盘文件,全部驻留在 Dnscache 进程内存;重启 Dnscache、重启电脑缓存全部清空。
 
ipconfig /displaydns 与 Get-DnsClientCache 数据源完全同源,只是展示格式不同。

2. DNS 缓存条目存储内容

每条缓存记录核心字段:
  • Name:查询域名
  • RecordType:A/AAAA/CNAME/MX/TXT 等记录类型
  • Data:解析结果 IP、别名等
  • Status:成功 / 否定缓存 (Negative Cache)
  • TTL / TimeToLive:剩余生存时间
  • InterfaceAlias:绑定网卡接口
  • Section:Answer / Authority / Additional
否定缓存(Negative Cache)
 
域名不存在、NXDOMAIN 结果同样缓存,避免重复向 DNS 服务器发送无效查询,降低负载;这也是某些域名改解析后依然无法访问的常见原因。

3. 查询工作模型

应用发起域名解析 → ws2_32.dll → 优先查询 Dnscache 共享内存缓存
  • 命中:直接返回结果,不发包到 DNS 服务器
  • 未命中:Dnscache 向配置 DNS 服务器递归查询,收到响应后写入内存缓存

二、依赖文件、注册表、系统组件

1. 核心二进制依赖

  1. DnsClient.dll
     
    PowerShell Get-DnsClientCache 底层调用的核心 Win32 API 库。
  2. Dnsapi.dll
     
    传统 DNS 解析 API,ipconfig /displaydns 使用此组件。
  3. dnscache.dll
     
    Dnscache 服务主模块,运行在 svchost.exe -k NetworkService
  4. 模块文件
     
    C:\Windows\System32\WindowsPowerShell\v1.0\Modules\DnsClient\DnsClient.psd1 / DnsClient.dll

2. 关键服务

服务名称:Dnscache(DNS Client)
⚠ 若停止 / 禁用 Dnscache:
  1. Get-DnsClientCache 直接返回空,无法读取系统缓存
  2. 系统级 DNS 共享缓存失效;进程只能使用 ws2_32 进程内短时缓存
  3. Windows 域、组策略、LLMNR、NRPT 策略依赖受损

3. 注册表配置(控制缓存行为)

路径:
 
HKLM\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters
 
常用参数:
  • MaxCacheTtl:正向记录最大缓存 TTL(秒)
  • MaxNegativeCacheTtl:否定缓存 TTL
  • NegativeCacheTime
  • DisableDNSPositiveCache / DisableDNSNegativeCache 关闭缓存
网卡 DNS 配置存储位置:
 
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}

4. 关联静态名称解析源(非缓存,但协同生效)

C:\Windows\System32\drivers\etc\hosts
 
工作逻辑:
 
DNS 查询优先级:hosts 文件 → Dnscache 内存缓存 → 外网 DNS 服务器
重点:hosts 匹配结果不会存入 Dnscache 缓存,Get-DnsClientCache 默认看不到hosts条目。

三、依赖关系梳理

plaintext
PowerShell 控制台
    ↓ 导入 DnsClient 模块
Get-DnsClientCache Cmdlet
    ↓ 调用 DnsClient.dll 封装API
    ↓ RPC调用 → svchost.exe(Dnscache服务)
    ↓ dnscache.dll 读取内部内存缓存表
    ↓ 缓存数据序列化返回PowerShell
    ↓ 输出 DnsClientCacheRecord 对象集合

强制依赖约束

  1. Dnscache 服务必须处于运行状态;服务停止 → 命令无有效缓存输出;
  2. 权限:普通用户即可读取缓存,不需要管理员权限;
  3. 协议依赖:该命令仅读取内存数据,不产生任何网络 DNS 请求;
  4. 版本依赖:DnsClient 模块 Win10 1607 之后内置;早期 Win10 无此 Cmdlet,只能使用 ipconfig。

上层业务依赖链

所有网络程序(浏览器、ssh.exe、OpenSSH、各类客户端)域名解析优先消耗该缓存;
 
tcpip.sys 不参与域名解析,域名解析由应用层 → dnsapi/dnscache 完成,再把 IP 交给 tcpip.sys 建立连接。

四、完整逻辑链路

链路 1:Get-DnsClientCache 执行链路

  1. 用户执行 Get-DnsClientCache
  2. PowerShell 加载 DnsClient.dll 托管 Cmdlet
  3. Cmdlet 发起本地 RPC 连接至 Dnscache 服务(svchost)
  4. dnscache 遍历进程内存内 DNS 缓存哈希表
  5. 将每条缓存记录封装为结构化对象回传给 PowerShell 运行时
  6. 格式化输出:Name、RecordType、TTL、Data、Status 等属性

链路 2:一条域名记录如何进入缓存

  1. 应用调用 getaddrinfo()(ws2_32)请求解析域名
  2. ws2_32 查询 Dnscache 共享缓存
  3. 缓存缺失 → Dnscache 向网卡 DNS 服务器发送 UDP 53 查询
  4. 获取 DNS 应答
  5. 根据 TTL 写入 Dnscache 内存哈希缓存
  6. 后续 Get-DnsClientCache 可以查询到这条记录

链路 3:缓存清除链路(配套操作)

powershell
Clear-DnsClientCache
  1. Cmdlet RPC 通知 Dnscache 服务
  2. dnscache 清空内存缓存哈希表
  3. 现存所有缓存条目全部失效
等价命令:ipconfig /flushdns

链路 4:典型故障场景

  1. Dnscache 服务被禁用 → Get-DnsClientCache 返回空列表;
  2. 修改 hosts,但无法在缓存中看到 hosts 域名(设计如此,hosts 不走 dnscache 缓存);
  3. 否定缓存生效:域名曾经 NXDOMAIN,短期内再次查询依然失败;
  4. NRPT 策略生效:DNS 查询被强制路由,缓存条目跟随策略生成;
  5. 多网卡环境:缓存条目绑定对应 InterfaceAlias,路由切换影响解析结果。

五、配套链(配套命令、运维工具、监控、排错体系)

1. 同模块配套 PowerShell 命令

powershell
# 查询DNS缓存
Get-DnsClientCache
# 清空系统DNS缓存
Clear-DnsClientCache
# 查看网卡DNS配置
Get-DnsClient
# 设置DNS服务器地址
Set-DnsClientServerAddress
# 查看DNSNRPT策略
Get-DnsClientNrptPolicy

2. 传统 CMD 配套工具

cmd
ipconfig /displaydns   # 读取同一缓存,文本展示
ipconfig /flushdns     # 清空缓存
nslookup                # 直接查询DNS服务器,**不读取本机缓存**
重要区分:nslookup 绕过 Dnscache,直接和 DNS 服务器通信,无法验证缓存状态。

3. 观测与排错配套

  1. 事件查看器:服务 Dnscache 日志
  2. 抓包 (Wireshark):观察 DNS 请求是否发出,判断缓存是否命中
  3. 性能监视器:DNS Client 性能计数器
  4. 防火墙:UDP53 出站影响缓存填充,但不影响缓存读取

4. 安全配套视角

  • 本地 DNS 缓存易遭受DNS 缓存投毒(本地);
  • 攻击者若篡改 hosts 优先级高于 DNS 缓存;
  • EDR 可监控 Clear-DnsClientCache、ipconfig /flushdns 行为(恶意程序常用反溯源手段)。

六、关键边界误区澄清

  1. ❌ 误区:DNS 缓存保存在文件
     
    ✅ 真相:全部在 Dnscache 内存,重启丢失;无持久存储文件。
  2. ❌ 误区:hosts 记录会出现在 Get-DnsClientCache
     
    ✅ 真相:hosts 由 ws2_32 直接读取,不进入 Dnscache 缓存。
  3. ❌ 误区:nslookup 可以测试缓存
     
    ✅ 真相:nslookup 独立实现解析,绕过系统 Dnscache。
  4. ❌ 误区:管理员才能读取缓存
     
    ✅ 真相:普通用户默认可读;清空缓存同样普通用户权限即可执行。

PowerShell 中与 DNS(Domain Name System)相关的命令按功能分类的表格。此表格展示了常用的 DNS(Domain Name System) 相关命令,并描述了它们的功能:

功能 PowerShell 命令 描述
查看 DNS 客户端缓存 Get-DnsClientCache 显示当前计算机上的 DNS 客户端缓存,包括域名和对应的 IP 地址。
清除 DNS 客户端缓存 Clear-DnsClientCache 清除 DNS 客户端缓存,强制计算机重新查询 DNS。
查看 DNS 配置 Get-DnsClient 显示当前计算机的 DNS 配置信息,例如 DNS 服务器、搜索域等。
设置 DNS 服务器 Set-DnsClientServerAddress 配置计算机的 DNS 服务器地址,可以为网络接口设置主/辅助 DNS 服务器。
查看 DNS 服务器配置 Get-DnsClientServerAddress 获取当前配置的 DNS 服务器的详细信息。
添加 DNS 服务器地址 Add-DnsClientServerAddress 向计算机的网络适配器添加新的 DNS 服务器地址。
删除 DNS 服务器地址 Remove-DnsClientServerAddress 从计算机的网络适配器中删除指定的 DNS 服务器地址。
查看 DNS 解析日志 Get-DnsServerLog 获取 DNS 服务器的解析日志信息(需要管理员权限)。
查看 DNS 服务器统计 Get-DnsServerStatistics 获取 DNS 服务器的统计信息,包括查询次数、错误等。
创建 DNS 区域 Add-DnsServerPrimaryZone 在 DNS 服务器上创建一个主 DNS 区域。
删除 DNS 区域 Remove-DnsServerZone 删除指定的 DNS 区域。
列出 DNS 区域中的记录 Get-DnsServerResourceRecord 查看 DNS 服务器中指定区域的资源记录(如 A 记录、CNAME 记录等)。
添加 DNS 记录 Add-DnsServerResourceRecordA 向 DNS 区域添加 A 记录(地址记录),将域名映射到 IP 地址。
删除 DNS 记录 Remove-DnsServerResourceRecord 删除指定 DNS 区域中的资源记录。
修改 DNS 记录 Set-DnsServerResourceRecord 修改 DNS 区域中的现有资源记录。

说明:

  • DNS 客户端功能:涉及计算机本地的 DNS(Domain Name System) 缓存和客户端配置。
  • DNS 服务器管理:用于管理DNS(Domain Name System)服务器设置,包括区域、记录和服务器日志等。

这些命令适用于 Windows PowerShell,通常用于管理员级别的 DNS(Domain Name System) 配置和管理。你可以通过 PowerShell 来执行 DNS(Domain Name System) 管理任务,而无需使用传统的 ipconfig 或 netsh 命令。


上述表格展示了与 DNS(Domain Name System) 相关的常用 PowerShell 命令。以下是每个命令的具体示例,以帮助你理解如何使用这些命令来进行 DNS(Domain Name System) 配置和管理。

1. 查看 DNS 客户端缓存

  • 命令:Get-DnsClientCache
  • 示例:
    powershellCopy Code
    Get-DnsClientCache
    该命令会列出本地计算机上缓存的 DNS 记录,包括域名和相应的 IP 地址。

2. 清除 DNS 客户端缓存

  • 命令:Clear-DnsClientCache
  • 示例:
    powershellCopy Code
    Clear-DnsClientCache
    清除本地计算机的 DNS 缓存,确保系统重新从 DNS 服务器获取域名解析结果。

3. 查看 DNS 配置

  • 命令:Get-DnsClient
  • 示例:
    powershellCopy Code
    Get-DnsClient
    该命令会显示计算机上所有网络适配器的 DNS 配置,包括 DNS 服务器地址和搜索域。

4. 设置 DNS 服务器

  • 命令:Set-DnsClientServerAddress
  • 示例:
    powershellCopy Code
    Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "8.8.8.8", "8.8.4.4"
    该命令为指定的网络适配器设置 DNS 服务器地址。在此示例中,为名为 "Ethernet" 的适配器配置了 Google 的 DNS 服务器地址。

5. 查看 DNS 服务器配置

  • 命令:Get-DnsClientServerAddress
  • 示例:
    powershellCopy Code
    Get-DnsClientServerAddress -InterfaceAlias "Ethernet"
    显示指定网络适配器的 DNS 服务器地址。

6. 添加 DNS 服务器地址

  • 命令:Add-DnsClientServerAddress
  • 示例:
    powershellCopy Code
    Add-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "1.1.1.1"
    向指定网络适配器的 DNS 配置中添加一个新的 DNS 服务器地址。

7. 删除 DNS 服务器地址

  • 命令:Remove-DnsClientServerAddress
  • 示例:
    powershellCopy Code
    Remove-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "8.8.8.8"
    从指定的网络适配器中删除指定的 DNS 服务器地址。

8. 查看 DNS 解析日志

  • 命令:Get-DnsServerLog
  • 示例:
    powershellCopy Code
    Get-DnsServerLog -ZoneName "example.com"
    显示指定区域(例如 "example.com")的 DNS 解析日志。

9. 查看 DNS 服务器统计信息

  • 命令:Get-DnsServerStatistics
  • 示例:
    powershellCopy Code
    Get-DnsServerStatistics
    获取当前 DNS 服务器的统计信息,如查询次数、错误数等。

10. 创建 DNS 区域

  • 命令:Add-DnsServerPrimaryZone
  • 示例:
    powershellCopy Code
    Add-DnsServerPrimaryZone -Name "example.com" -ZoneFile "example.com.dns"
    创建一个主 DNS 区域 example.com,并指定一个区域文件。

11. 删除 DNS 区域

  • 命令:Remove-DnsServerZone
  • 示例:
    powershellCopy Code
    Remove-DnsServerZone -Name "example.com"
    删除名为 "example.com" 的 DNS 区域。

12. 列出 DNS 区域中的记录

  • 命令:Get-DnsServerResourceRecord
  • 示例:
    powershellCopy Code
    Get-DnsServerResourceRecord -ZoneName "example.com"
    显示 example.com 区域中的所有资源记录(如 A 记录、CNAME 记录等)。

13. 添加 DNS 记录

  • 命令:Add-DnsServerResourceRecordA
  • 示例:
    powershellCopy Code
    Add-DnsServerResourceRecordA -Name "www" -ZoneName "example.com" -IPv4Address "192.168.1.1"
    向 example.com 区域添加一个 A 记录,将 www.example.com 映射到 IP 地址 192.168.1.1。

14. 删除 DNS 记录

  • 命令:Remove-DnsServerResourceRecord
  • 示例:
    powershellCopy Code
    Remove-DnsServerResourceRecord -ZoneName "example.com" -RRType "A" -Name "www"
    删除 example.com 区域中的 www A 记录。

15. 修改 DNS 记录

  • 命令:Set-DnsServerResourceRecord
  • 示例:
    powershellCopy Code
    Set-DnsServerResourceRecord -ZoneName "example.com" -Name "www" -NewIPv4Address "192.168.1.2"
    修改 example.com 区域中的 www A 记录,将其 IP 地址从 192.168.1.1 修改为 192.168.1.2。

这些示例展示了如何在 PowerShell 中使用命令来管理和配置 DNS 客户端以及 DNS 服务器。根据实际需求,你可以选择执行不同的操作,例如查看、配置、清除 DNS 缓存,或者添加、删除 DNS 记录等。


 

常用 DNS 相关 PowerShell 命令,以下是一些补充和扩展的命令,这些命令可以帮助你进行更详细和高级的 DNS 配置、管理和排错工作。

1. 查看 DNS 服务器列表

  • 命令:Get-DnsServer
  • 示例:
    powershellCopy Code
    Get-DnsServer
    该命令显示当前系统上配置的 DNS 服务器信息。

2. 查看 DNS 服务器的转发器

  • 命令:Get-DnsServerForwarder
  • 示例:
    powershellCopy Code
    Get-DnsServerForwarder
    显示 DNS 服务器的转发器设置,转发器用于将无法解析的 DNS 请求转发给其他 DNS 服务器。

3. 添加 DNS 转发器

  • 命令:Add-DnsServerForwarder
  • 示例:
    powershellCopy Code
    Add-DnsServerForwarder -IPAddress "8.8.8.8"
    向 DNS 服务器添加一个转发器地址,在此示例中,设置了 Google DNS 服务器(8.8.8.8)作为转发器。

4. 删除 DNS 转发器

  • 命令:Remove-DnsServerForwarder
  • 示例:
    powershellCopy Code
    Remove-DnsServerForwarder -IPAddress "8.8.8.8"
    从 DNS 服务器中移除一个转发器地址。

5. 查看 DNS 服务器的根提示

  • 命令:Get-DnsServerRootHint
  • 示例:
    powershellCopy Code
    Get-DnsServerRootHint
    显示当前 DNS 服务器的根提示信息,根提示通常是根 DNS 服务器的 IP 地址。

6. 添加 DNS 根提示

  • 命令:Add-DnsServerRootHint
  • 示例:
    powershellCopy Code
    Add-DnsServerRootHint -Name "a.root-servers.net." -IPAddress "198.41.0.4"
    向 DNS 服务器的根提示列表中添加一个新的根 DNS 服务器地址。

7. 删除 DNS 根提示

  • 命令:Remove-DnsServerRootHint
  • 示例:
    powershellCopy Code
    Remove-DnsServerRootHint -Name "a.root-servers.net."
    从 DNS 服务器的根提示列表中删除指定的根 DNS 服务器。

8. 查看 DNS 服务器的区域转发设置

  • 命令:Get-DnsServerZoneDelegation
  • 示例:
    powershellCopy Code
    Get-DnsServerZoneDelegation -ZoneName "example.com"
    查看指定 DNS 区域的委派信息。

9. 添加 DNS 区域委派

  • 命令:Add-DnsServerZoneDelegation
  • 示例:
    powershellCopy Code
    Add-DnsServerZoneDelegation -Name "subdomain.example.com" -NameServers "ns1.subdomain.example.com", "ns2.subdomain.example.com"
    向父区域(例如 example.com)添加一个子区域(例如 subdomain.example.com)的委派。

10. 删除 DNS 区域委派

  • 命令:Remove-DnsServerZoneDelegation
  • 示例:
    powershellCopy Code
    Remove-DnsServerZoneDelegation -ZoneName "example.com" -Name "subdomain.example.com"
    删除 DNS 区域的委派记录。

11. 查看 DNS 服务器的转发规则

  • 命令:Get-DnsServerZone
  • 示例:
    powershellCopy Code
    Get-DnsServerZone
    显示当前 DNS 服务器上配置的所有区域。

12. 设置 DNS 服务器区域的更新策略

  • 命令:Set-DnsServerZoneAging
  • 示例:
    powershellCopy Code
    Set-DnsServerZoneAging -ZoneName "example.com" -AgingEnabled $true -NoRefreshInterval 7 -RefreshInterval 30
    配置 DNS 区域的老化策略。启用老化后,DNS 记录将在特定条件下过期,帮助清理过时的记录。

13. 查看 DNS 服务器的区域日志

  • 命令:Get-DnsServerZoneAudit
  • 示例:
    powershellCopy Code
    Get-DnsServerZoneAudit -ZoneName "example.com"
    查看 DNS 区域的日志,帮助分析和审计 DNS 更新和查询记录。

14. 创建 DNS CNAME 记录

  • 命令:Add-DnsServerResourceRecordCName
  • 示例:
    powershellCopy Code
    Add-DnsServerResourceRecordCName -Name "mail" -ZoneName "example.com" -HostNameAlias "mailserver.example.com"
    为 example.com 区域添加 CNAME 记录,指向 mailserver.example.com。

15. 创建 DNS MX 记录

  • 命令:Add-DnsServerResourceRecordMX
  • 示例:
    powershellCopy Code
    Add-DnsServerResourceRecordMX -Name "example.com" -MailExchange "mail.example.com" -Preference 10
    为 example.com 区域添加 MX 记录,用于邮件交换。

16. 查看 DNS 服务器的全局查询统计信息

  • 命令:Get-DnsServerQueryStatistics
  • 示例:
    powershellCopy Code
    Get-DnsServerQueryStatistics
    获取 DNS 服务器的查询统计信息,如查询的数量、类型等。

17. 清除 DNS 服务器的缓存

  • 命令:Clear-DnsServerCache
  • 示例:
    powershellCopy Code
    Clear-DnsServerCache
    清除 DNS 服务器的缓存,强制重新查询域名解析。

18. 列出 DNS 服务器的所有资源记录

  • 命令:Get-DnsServerResourceRecord
  • 示例:
    powershellCopy Code
    Get-DnsServerResourceRecord -ZoneName "example.com"
    获取指定区域的所有 DNS 资源记录,支持查看各种类型的记录(A 记录、CNAME 记录、MX 记录等)。

19. 查看 DNS 服务器的运行状态

  • 命令:Get-DnsServerStatus
  • 示例:
    powershellCopy Code
    Get-DnsServerStatus
    获取 DNS 服务器的状态信息,包括是否正在运行、服务是否正常等。

20. 验证 DNS 服务器的运行状况

  • 命令:Test-DnsServer
  • 示例:
    powershellCopy Code
    Test-DnsServer -Name "example.com"
    测试 DNS 服务器是否能够解析指定的域名(在本示例中是 example.com)。

 

这些命令可以帮助你进一步管理和配置 DNS 系统,包括更复杂的操作,如区域转发、记录管理、服务器配置和日志分析等。如果你需要详细操作和排查 DNS 问题,了解这些命令非常重要。通过 PowerShell,你可以灵活地自动化和管理 DNS 环境,增强系统的可靠性和效率。

 

DNS 管理 和 配置 DNS 系统 的功能分类的 PowerShell 命令表格:

功能分类 命令 说明 示例
查看 DNS 配置 Get-DnsServer 查看当前系统上配置的 DNS 服务器信息 Get-DnsServer
  Get-DnsServerForwarder 查看 DNS 服务器的转发器设置 Get-DnsServerForwarder
  Get-DnsServerRootHint 查看 DNS 服务器的根提示 Get-DnsServerRootHint
  Get-DnsServerZone 查看 DNS 服务器的区域信息 Get-DnsServerZone
  Get-DnsServerZoneAudit 查看 DNS 服务器的区域日志 Get-DnsServerZoneAudit -ZoneName "example.com"
DNS 区域管理 Get-DnsServerZone 查看当前 DNS 服务器的所有区域 Get-DnsServerZone
  Add-DnsServerZone 创建一个新的 DNS 区域 Add-DnsServerZone -Name "example.com" -ZoneType Primary
  Set-DnsServerZoneAging 设置 DNS 区域的老化策略 Set-DnsServerZoneAging -ZoneName "example.com" -AgingEnabled $true -NoRefreshInterval 7 -RefreshInterval 30
  Remove-DnsServerZone 删除指定的 DNS 区域 Remove-DnsServerZone -Name "example.com"
DNS 记录管理 Get-DnsServerResourceRecord 查看 DNS 区域的所有资源记录 Get-DnsServerResourceRecord -ZoneName "example.com"
  Add-DnsServerResourceRecordA 添加 A 记录(IPv4 地址) Add-DnsServerResourceRecordA -Name "www" -ZoneName "example.com" -IPv4Address "192.168.1.10"
  Add-DnsServerResourceRecordCName 添加 CNAME 记录(别名记录) Add-DnsServerResourceRecordCName -Name "mail" -ZoneName "example.com" -HostNameAlias "mailserver.example.com"
  Add-DnsServerResourceRecordMX 添加 MX 记录(邮件交换记录) Add-DnsServerResourceRecordMX -Name "example.com" -MailExchange "mail.example.com" -Preference 10
  Remove-DnsServerResourceRecord 删除指定的 DNS 资源记录 Remove-DnsServerResourceRecord -Name "www" -ZoneName "example.com"
DNS 转发器管理 Get-DnsServerForwarder 查看 DNS 服务器的转发器列表 Get-DnsServerForwarder
  Add-DnsServerForwarder 添加 DNS 转发器 Add-DnsServerForwarder -IPAddress "8.8.8.8"
  Remove-DnsServerForwarder 删除 DNS 转发器 Remove-DnsServerForwarder -IPAddress "8.8.8.8"
根提示管理 Get-DnsServerRootHint 查看 DNS 服务器的根提示 Get-DnsServerRootHint
  Add-DnsServerRootHint 添加根提示记录 Add-DnsServerRootHint -Name "a.root-servers.net." -IPAddress "198.41.0.4"
  Remove-DnsServerRootHint 删除指定的根提示记录 Remove-DnsServerRootHint -Name "a.root-servers.net."
DNS 委派管理 Get-DnsServerZoneDelegation 查看 DNS 区域的委派信息 Get-DnsServerZoneDelegation -ZoneName "example.com"
  Add-DnsServerZoneDelegation 向父区域添加子区域委派 Add-DnsServerZoneDelegation -Name "subdomain.example.com" -NameServers "ns1.subdomain.example.com", "ns2.subdomain.example.com"
  Remove-DnsServerZoneDelegation 删除 DNS 区域的委派 Remove-DnsServerZoneDelegation -ZoneName "example.com" -Name "subdomain.example.com"
DNS 查询和缓存管理 Get-DnsServerQueryStatistics 获取 DNS 服务器的查询统计信息 Get-DnsServerQueryStatistics
  Clear-DnsServerCache 清除 DNS 服务器的缓存 Clear-DnsServerCache
DNS 状态管理 Get-DnsServerStatus 获取 DNS 服务器的运行状态信息 Get-DnsServerStatus
  Test-DnsServer 测试 DNS 服务器的健康状况,验证域名解析 Test-DnsServer -Name "example.com"
DNS 资源记录更新 Set-DnsServerResourceRecord 更新 DNS 资源记录 Set-DnsServerResourceRecord -Name "www" -ZoneName "example.com" -IPv4Address "192.168.1.20"

 

这个表格按功能分类了常见的 PowerShell 命令,帮助你更高效地管理和配置 DNS 系统。通过这些命令,可以进行从 DNS 区域管理、记录管理到转发器和根提示的配置等操作,极大提升 DNS 管理的灵活性和自动化能力。

 


使用 PowerShell 管理 DNS 服务器,你可以执行多种操作,如添加、删除和修改 DNS 记录,以及管理 DNS 区域。以下是一些常用的 cmdlet 示例:

查看所有 DNS 区域

powershellCopy Code
Get-DnsServerZone

添加新的 DNS 区域

powershellCopy Code
Add-DnsServerPrimaryZone -Name "yourdomain.com" -ReplicationScope "Domain"

添加 A 记录

powershellCopy Code
Add-DnsServerResourceRecordA -Name "example" -ZoneName "yourdomain.com" -IPv4Address "192.168.1.1"

删除 DNS 区域

powershellCopy Code
Remove-DnsServerZone -Name "yourdomain.com" -Force

修改 DNS 记录

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -IPv4Address "192.168.1.2" -OldIPv4Address "192.168.1.1" -RecordType "A"

查询 DNS 记录

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com"

更高级的 DNS 管理操作示例:

导出 DNS 区域

powershellCopy Code
Export-DnsServerZone -Name "yourdomain.com" -File "C:\path\to\exported_zone.dns"

从文件导入 DNS 区域

powershellCopy Code
Import-DnsServerZone -File "C:\path\to\exported_zone.dns" -ZoneName "yourdomain.com"

设置 DNS 记录的生存时间(TTL)

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -TimeToLive "01:00:00" -RecordType "A"

查看特定 A 记录

powershellCopy Code
Get-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType "A"

清除 DNS 缓存

powershellCopy Code
Clear-DnsServerCache

更深入的操作和示例:

列出所有 DNS 记录类型

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com"

批量添加多个 A 记录

powershellCopy Code
$records = @(
    @{Name="example1"; IPv4Address="192.168.1.10"},
    @{Name="example2"; IPv4Address="192.168.1.11"}
)
foreach ($record in $records) {
    Add-DnsServerResourceRecordA -Name $record.Name -ZoneName "yourdomain.com" -IPv4Address $record.IPv4Address
}

查看 DNS 服务器的配置

powershellCopy Code
Get-DnsServer

修改 DNS 区域的复制范围

powershellCopy Code
Set-DnsServerPrimaryZone -Name "yourdomain.com" -ReplicationScope "Forest"

设置 DNS 记录的备注

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -Comment "This is a test record"

更多的 DNS 管理操作以及相关示例:

删除特定的 A 记录

powershellCopy Code
Remove-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType "A" -Force

创建 CNAME 记录

powershellCopy Code
Add-DnsServerResourceRecordCNAME -Name "alias" -ZoneName "yourdomain.com" -HostNameAlias "target.yourdomain.com"

创建 MX 记录

powershellCopy Code
Add-DnsServerResourceRecordMX -ZoneName "yourdomain.com" -MailExchange "mailserver.yourdomain.com" -Preference 10

查看所有 DNS 服务器的缓存

powershellCopy Code
Get-DnsServerCache

启用或禁用 DNS 服务器的递归查询

powershellCopy Code
Set-DnsServerRecursion -Enable $true  # 启用递归
Set-DnsServerRecursion -Enable $false # 禁用递归

查看 DNS 服务器的事件日志

powershellCopy Code
Get-WinEvent -LogName "Microsoft-Windows-DNS-Server/Analytic"

监控 DNS 服务器性能

使用以下命令获取 DNS 服务器的性能计数器:

powershellCopy Code
Get-Counter -Counter "\DNS Server(*)"

还有一些高级 DNS 管理操作:

设置 DNS 记录的优先级

powershellCopy Code
Set-DnsServerResourceRecordMX -Name "yourdomain.com" -MailExchange "mailserver.yourdomain.com" -Preference 5

复制 DNS 区域到其他 DNS 服务器

powershellCopy Code
Invoke-DnsServerZoneTransfer -ZoneName "yourdomain.com" -DestinationServer "SecondaryDNS"

创建 PTR 记录(反向解析)

powershellCopy Code
Add-DnsServerResourceRecordPtr -Name "10.1.168.192.in-addr.arpa" -PtrDomainName "example.yourdomain.com" -ZoneName "1.168.192.in-addr.arpa"

修改 DNS 记录的时间戳

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -Timestamp (Get-Date)

查看域名的 WHOIS 信息

虽然这不是 DNS 管理命令,但可以使用以下命令获取 WHOIS 信息(需要安装相应工具):

powershellCopy Code
whois yourdomain.com

 


有更多 DNS 管理命令和示例:

创建 SRV 记录

powershellCopy Code
Add-DnsServerResourceRecordSRV -Name "_sip._tcp" -ZoneName "yourdomain.com" -Priority 10 -Weight 5 -Port 5060 -Target "sipserver.yourdomain.com"

导出 DNS 区域到文件

powershellCopy Code
Export-DnsServerZone -ZoneName "yourdomain.com" -File "C:\path\to\exported_zone.dns"

从文件导入 DNS 区域

powershellCopy Code
Import-DnsServerZone -ZoneFile "C:\path\to\imported_zone.dns" -ZoneName "yourdomain.com"

列出 DNS 服务器的所有区域

powershellCopy Code
Get-DnsServerZone

设置 DNS 服务器的转发器

powershellCopy Code
Add-DnsServerForwarder -IPAddress "8.8.8.8"

清除 DNS 缓存

powershellCopy Code
Clear-DnsServerCache

 


还有一些有用的 DNS 管理命令和操作示例:

更新 A 记录

powershellCopy Code
Set-DnsServerResourceRecordA -Name "example" -ZoneName "yourdomain.com" -IPv4Address "192.168.1.10" -Force

列出特定区域的所有记录

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com"

删除整个 DNS 区域

powershellCopy Code
Remove-DnsServerZone -Name "yourdomain.com" -Force

查看特定 DNS 记录的详细信息

powershellCopy Code
Get-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com"

设置 DNS 记录的 TTL

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -TimeToLive (New-TimeSpan -Hours 1) -Force

检查域名的解析结果

使用 Resolve-DnsName 命令:

powershellCopy Code
Resolve-DnsName -Name "example.yourdomain.com"

查看 DNS 服务器的配置

powershellCopy Code
Get-DnsServer

 


有更多的 DNS 管理命令和技巧:

创建 CNAME 记录

powershellCopy Code
Add-DnsServerResourceRecordCname -Name "www" -HostNameAlias "example.yourdomain.com" -ZoneName "yourdomain.com"

更新 MX 记录

powershellCopy Code
Set-DnsServerResourceRecordMX -Name "yourdomain.com" -MailExchange "newmailserver.yourdomain.com" -Preference 10 -Force

删除特定的 DNS 记录

powershellCopy Code
Remove-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -Force

设置 DNS 服务器的代理

powershellCopy Code
Set-DnsServerForwarder -IPAddress "1.1.1.1" -PassThru

列出 DNS 服务器的转发器

powershellCopy Code
Get-DnsServerForwarder

查询 DNS 记录的历史

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name "example" -ComputerName "dnsserver" -ZoneScope "ScopeName"

 


更多 DNS 管理命令:

创建 PTR 记录(反向解析)

powershellCopy Code
Add-DnsServerResourceRecordPtr -Name "10.1.168.192.in-addr.arpa" -PtrDomainName "example.yourdomain.com" -ZoneName "1.168.192.in-addr.arpa"

设置 SRV 记录的优先级

powershellCopy Code
Set-DnsServerResourceRecordSRV -Name "_sip._tcp" -ZoneName "yourdomain.com" -Priority 20 -Force

列出 DNS 记录类型

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" | Select-Object RecordType

创建 TXT 记录

powershellCopy Code
Add-DnsServerResourceRecordTxt -Name "example" -ZoneName "yourdomain.com" -Text "v=spf1 include:_spf.example.com ~all"

批量导入 DNS 记录

你可以通过 CSV 文件批量导入记录,格式为:

Copy Code
Name,RecordType,RecordData
example,yourdomain.com,A,192.168.1.10

然后使用脚本读取和导入。


更多的 DNS 管理命令和技巧:

导出 DNS 区域记录到文件

powershellCopy Code
Export-DnsServerZone -Name "yourdomain.com" -File "C:\path\to\your\zonefile.dns"

导入 DNS 区域记录从文件

powershellCopy Code
Import-DnsServerZone -ZoneFile "C:\path\to\your\zonefile.dns" -ZoneName "yourdomain.com"

列出 DNS 服务器上的所有区域

powershellCopy Code
Get-DnsServerZone

查看特定区域的 DNS 统计信息

powershellCopy Code
Get-DnsServerZone -Name "yourdomain.com" | Format-List

检查 DNS 记录是否存在

powershellCopy Code
if (-not (Get-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -ErrorAction SilentlyContinue)) {
    Write-Output "记录不存在"
}

设置 DNS 服务器的安全性

powershellCopy Code
Set-DnsServerZone -Name "yourdomain.com" -ZoneType Primary -SecureDelegation $true

清除 DNS 缓存

powershellCopy Code
Clear-DnsServerCache

查看 DNS 缓存内容

powershellCopy Code
Get-DnsServerCache

 


提供更多的 DNS 管理命令和技巧:

设置 DNS 区域的转发

powershellCopy Code
Set-DnsServerZone -Name "yourdomain.com" -Forwarders "8.8.8.8","8.8.4.4"

获取 DNS 区域的转发设置

powershellCopy Code
Get-DnsServerZone -Name "yourdomain.com" | Select-Object -ExpandProperty Forwarders

创建 SOA 记录

powershellCopy Code
Add-DnsServerResourceRecordSoa -Name "yourdomain.com" -Host "ns1.yourdomain.com" -Email "admin@yourdomain.com" -Serial 1 -RefreshInterval 3600 -RetryInterval 1800 -ExpireInterval 604800 -MinimumTtl 3600

设置 DNS 记录的 TTL(生存时间)

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -TimeToLive 01:00:00 -Force

列出 DNS 记录的详细信息

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name "example" | Format-List

检查域名解析

powershellCopy Code
Resolve-DnsName -Name "www.yourdomain.com"

监控 DNS 服务器性能

powershellCopy Code
Get-Counter -Counter "\DNS Server(*)"

定期备份 DNS 区域

你可以使用计划任务定期运行导出命令:

powershellCopy Code
Export-DnsServerZone -Name "yourdomain.com" -File "C:\path\to\backup\zonefile_$(Get-Date -Format 'yyyyMMddHHmm').dns"

 


更多的 DNS 管理命令和技巧:

删除 DNS 记录

powershellCopy Code
Remove-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -Force

批量导入 DNS 记录

可以使用 CSV 文件导入多个记录:

powershellCopy Code
Import-Csv "C:\path\to\records.csv" | ForEach-Object {
    Add-DnsServerResourceRecordA -Name $_.Name -IPv4Address $_.IPAddress -ZoneName "yourdomain.com"
}

创建 CNAME 记录

powershellCopy Code
Add-DnsServerResourceRecordCname -Name "alias" -HostNameAlias "target.yourdomain.com" -ZoneName "yourdomain.com"

获取 DNS 记录的历史

如果启用了 DNS 记录版本控制,可以查看历史记录:

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name "example" -ChangeHistory

设置 DNS 记录的优先级(用于 MX 记录)

powershellCopy Code
Add-DnsServerResourceRecordMx -Name "yourdomain.com" -MailExchange "mail.yourdomain.com" -Preference 10

查看 DNS 服务器的配置

powershellCopy Code
Get-DnsServer

记录 DNS 服务器的事件日志

powershellCopy Code
Get-WinEvent -LogName "DNS Server"

监测 DNS 查询

可以使用命令监测实时 DNS 查询:

powershellCopy Code
Get-DnsServerQuery

 


更多的 DNS 管理命令和技巧:

修改现有 DNS 记录

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -IPv4Address "192.0.2.1" -Force

查询特定类型的 DNS 记录

你可以查询特定类型的记录,例如 TXT 记录:

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name "example" -RecordType TXT

创建 SRV 记录

powershellCopy Code
Add-DnsServerResourceRecordSrv -Name "_sip._tcp" -ZoneName "yourdomain.com" -Priority 10 -Weight 5 -Port 5060 -Target "sipserver.yourdomain.com"

列出所有 DNS 记录

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com"

导出 DNS 区域到文件

powershellCopy Code
Export-DnsServerZone -Name "yourdomain.com" -File "C:\path\to\backup\zonefile.dns"

导入 DNS 区域

powershellCopy Code
Import-DnsServerZone -ZoneFile "C:\path\to\backup\zonefile.dns" -Name "yourdomain.com"

清理过期的 DNS 记录

powershellCopy Code
Invoke-DnsServerCleanup -ZoneName "yourdomain.com"

设置 DNS 服务器的转发器

powershellCopy Code
Set-DnsServerForwarder -IPAddress "8.8.8.8"

检查 DNS 服务器的状态

powershellCopy Code
Get-DnsServerStatus

监控 DNS 请求的性能

powershellCopy Code
Get-Counter "\DNS Server\Queries/sec"

 


更多关于 DNS 管理的命令和技巧:

重新启动 DNS 服务器服务

如果需要重启 DNS 服务,可以使用以下命令:

powershellCopy Code
Restart-Service -Name "DNS"

刷新 DNS 缓存

如果你需要清除 DNS 服务器的缓存,可以使用:

powershellCopy Code
Clear-DnsServerCache

配置 DNS 服务器的区域安全性

powershellCopy Code
Set-DnsServerZone -Name "yourdomain.com" -ZoneType Secure

创建区域委派

如果需要将某个子域委派给其他 DNS 服务器:

powershellCopy Code
Add-DnsServerZoneDelegation -Name "subdomain" -ZoneName "yourdomain.com" -ChildZone "subdomain.yourdomain.com" -NameServers "ns1.otherdomain.com"

设置 DNS 记录的 TTL(生存时间)

powershellCopy Code
Set-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -TimeToLive 3600 -IPv4Address "192.0.2.1" -Force

显示 DNS 服务器的转发器

powershellCopy Code
Get-DnsServerForwarder

导出所有 DNS 记录到 CSV 文件

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" | Export-Csv "C:\path\to\backup\records.csv" -NoTypeInformation

导入 DNS 记录从 CSV 文件(带 TTL)

powershellCopy Code
Import-Csv "C:\path\to\backup\records.csv" | ForEach-Object {
    Add-DnsServerResourceRecordA -Name $_.Name -IPv4Address $_.IPv4Address -ZoneName "yourdomain.com" -TimeToLive $_.TTL
}

查询 DNS 记录的详细信息

powershellCopy Code
Get-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name "example" -Detailed

配置 DNS 服务器的转发策略

powershellCopy Code
Set-DnsServerForwarder -Name "Forwarder1" -IPAddress "8.8.4.4"

检查 DNS 服务器的性能指标

powershellCopy Code
Get-Counter -Counter "\DNS Server\% of Total Queries"

 


更多的 DNS 管理命令和技巧:

创建 CNAME 记录

powershellCopy Code
Add-DnsServerResourceRecordCNAME -Name "www" -HostNameAlias "example.com" -ZoneName "yourdomain.com"

删除 DNS 记录

powershellCopy Code
Remove-DnsServerResourceRecord -Name "example" -ZoneName "yourdomain.com" -RecordType A -Force

设置区域的最小 TTL

powershellCopy Code
Set-DnsServerZone -Name "yourdomain.com" -MinimumTTL 3600

显示 DNS 服务器的区域

powershellCopy Code
Get-DnsServerZone

创建 MX 记录

powershellCopy Code
Add-DnsServerResourceRecordMX -Name "yourdomain.com" -MailExchange "mailserver.yourdomain.com" -Preference 10

查询根 DNS 服务器

powershellCopy Code
Resolve-DnsName -Name "example.com" -Type NS

创建 PTR 记录(反向解析)

powershellCopy Code
Add-DnsServerResourceRecordPTR -Name "1.2.0.192.in-addr.arpa" -PtrDomainName "example.com"

配置 DNS 服务器的转发器

powershellCopy Code
Set-DnsServerForwarder -IPAddress "1.1.1.1"

显示 DNS 记录的变更历史

powershellCopy Code
Get-DnsServerResourceRecordChange -ZoneName "yourdomain.com"

备份整个 DNS 配置

powershellCopy Code
Export-DnsServerConfiguration -Path "C:\path\to\backup\config.bak"

 


 

posted @ 2024-09-19 14:40  suv789  阅读(1084)  评论(0)    收藏  举报