如何使用 PowerShell 获取 DNS IP 设置?

powershellmicrosoft technologiessoftware & coding更新于 2026/2/9 22:07:17

Ipconfig /all 命令还可以检索所有网络接口的 DNS 设置。此命令可以在 cmd 和 PowerShell 中运行。例如:

示例

PS C:\Users\Administrator> ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : Test1-Win2k16
   Primary Dns Suffix  . . . . . . . : labdomain.local
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : labdomain.local

Ethernet adapter Ethernet0:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) 82574L Gigabit Network Connection
   Physical Address. . . . . . . . . : 00-0C-29-E1-28-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 192.168.0.108(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1
   DHCPv6 IAID . . . . . . . . . . . : 33557545
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-26-A9-34-58-00-0C-29-E1-28-E0
   DNS Servers . . . . . . . . . . . : 192.168.0.105
   NetBIOS over Tcpip. . . . . . . . : Enabled

Tunnel adapter isatap.{5F9A3612-A410-4408-A7A8-368D2E16D6A8}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

但此命令的问题在于您无法正确筛选结果。例如,如果您需要检索特定接口的信息,则需要在 PowerShell 中编写大量字符串操作代码。

DNS 客户端设置可用的 GET 动词命令很少。让我们来检查一下。

Get-Command -Verb Get -Noun DNS*

Name                       Version
----                       -------
Get-DnsClient              1.0.0.0
Get-DnsClientCache         1.0.0.0
Get-DnsClientGlobalSetting 1.0.0.0
Get-DnsClientNrptGlobal    1.0.0.0
Get-DnsClientNrptPolicy    1.0.0.0
Get-DnsClientNrptRule      1.0.0.0
Get-DnsClientServerAddress 1.0.0.0

要检索与 DNS 客户端 IP 设置相关的数据(包括域名),例如 Ipconfig /all 命令,我们主要需要 3 个命令。

  • Get-DnsClient

  • Get-DnsClientGlobalSetting

  • Get-DnsClientServerAddress

我们将逐一介绍每个命令。

Get-DnsClient

此命令获取特定计算机上配置的特定网络接口的详细信息。当 Set-DnsClientServerAddress 命令以流水线方式运行时,此命令还可以帮助您在客户端计算机上设置 DNS 服务器地址。

在本地计算机上运行此命令时,它会提供本地接口的详细信息。

Get-DnsClient

如果您需要特定的接口地址信息,请使用 -InterfaceIndex 参数。在上述输出接口索引中,3 是主适配器。

Get-DnsClient -InterfaceIndex 3

输出

为了在远程服务器上获取相同的设置,我们可以使用 -CimSession 参数。

$sess=New-CimSession-ComputerNameTest1-Win2k16
Get-DnsClient-Session$sess

Get-DNSClientGlobalSetting

此 cmdlet 检索所有接口共用的 DNS 客户端设置,例如 DNS 后缀搜索列表。运行该命令后,输出如下所示。

输出

PS C:\Users\Administrator>Get-DnsClientGlobalSetting

UseSuffixSearchList: True
SuffixSearchList: {labdomain.local}
UseDevolution : True
DevolutionLevel : 0

要获取远程服务器上的设置,请使用 CIM 会话参数 -Session。

$sess=New-CimSession-ComputerNameTest1-Win2k16
Get-DnsClientGlobalSetting-Session$sess

UseSuffixSearchList: True
SuffixSearchList: {labdomain.local}
UseDevolution : True
DevolutionLevel : 0
PSComputerName : Test1-Win2k16

Get-DnsClientServerAddress

此 cmdlet 检索与计算机上的接口关联的一个或多个 DNS 地址。例如:

示例

Get-DnsClientServerAddress

PS C:\Users\Administrator> Get-DnsClientServerAddress

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet0                            3 IPv4    {192.168.0.106}
Ethernet0                            3 IPv6    {}
Loopback Pseudo-Interface 1          1 IPv4    {}
isatap.{5F9A3612-A410-440...         4 IPv4    {192.168.0.106}
isatap.{5F9A3612-A410-440...         4 IPv6    {}

In the above output, the main interface Ethernet0 has associated DNS address is 192.168.0.106. Likewise, there are different IPv4 and IPv6 interfaces, and their DNS addresses are displayed in the Server Address field.

To retrieve only IPv4 interface associated DNS server address, use -AddressFamily parameter.

Get-DnsClientServerAddress -AddressFamily IPv4

输出


InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet0                            3 IPv4    {192.168.0.106}
Loopback Pseudo-Interface 1          1 IPv4    {}
isatap.{5F9A3612-A410-440...         4 IPv4    {192.168.0.106}

To get the DNS server IPs of the specific interface, you need to use its index by supplying index to the -InterfaceIndex Parameter.

Get-DnsClientServerAddress -InterfaceIndex 3

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet0                            3 IPv4    {192.168.0.106}
Ethernet0                            3 IPv6    {}

To get the DNS server list on the remote system, you need to use the CIM session parameter -Session.

Get-DnsClientServerAddress -AddressFamily IPv4 -Session $sess

输出

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet0                            3 IPv4    {192.168.0.106}
Loopback Pseudo-Interface 1          1 IPv4    {}
isatap.{5F9A3612-A410-440...         4 IPv4    {192.168.0.106}

相关文章