如何使用 PowerShell 獲取 DNS IP 設定?
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 -ComputerName Test1-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 -ComputerName Test1-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 {}
在上面的輸出中,主介面 Ethernet0 關聯的 DNS 地址是 192.168.0.106。同樣,還有不同的 IPv4 和 IPv6 介面,它們的 DNS 地址顯示在伺服器地址欄位中。
要僅檢索與 IPv4 介面關聯的 DNS 伺服器地址,請使用 -AddressFamily 引數。
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}
要獲取特定介面的 DNS 伺服器 IP,您需要透過向 -InterfaceIndex 引數提供索引來使用其索引。
Get-DnsClientServerAddress -InterfaceIndex 3 InterfaceAlias Interface Address ServerAddresses Index Family -------------- --------- ------- --------------- Ethernet0 3 IPv4 {192.168.0.106} Ethernet0 3 IPv6 {}
要在遠端系統上獲取 DNS 伺服器列表,您需要使用 CIM 會話引數 -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}