The third post in my PowerShell Beginner series, “Daily Tasks, The PowerShell Way“. Use PowerShell to replace ipconfig‘s /registerdns, /displaydns, and /flushdns functions, and use for both local and remote computers.
ipconfig: a longtime member of the IT Pro toolbox
Much like ping, ipconfig was a frequently used command line tool for IT Pros. Whether it was getting assigned IP information, releasing and renewing DHCP leases, or investigating DNS client issues, ipconfig was quick go to troubleshooting tool. However, recent versions of Windows and PowerShell have added easy to use cmdlets to replace some of these functions. This post will take a look at the cmdlets related to the DNS client.
Register-DnsClient replaces ipconfig /registerdns
When a Windows system connects to the network, whether via DHCP or static IP, it will attempt to register with the DNS servers that have been configured for it. Windows DNS servers support dynamic updates from Windows DNS clients, so A records for a host can be created and maintained automatically.
DHCP clients are pretty good about registering with DNS with each new lease renewal. The DHCP Client Service is actually the service responsible for sending dynamic updates to the DNS server, even if an interface isn’t configured for DHCP. However, I have encountered the problem more than once where a computer configured with a static IP doesn’t regularly send an update to the DNS server, and the DNS record phases out, leaving the computer without an associated DNS record.
With ipconfig, you could trigger a dynamic update using the /registerdns option on the client computer. In PowerShell, this function has been replaced with the Register-DnsClient cmdlet. Simply run Register-DnsClient and the dynamic update process is triggered (the -Verbose switch has been included to show additional messaging).
PS C:\Users\aaron> Register-DnsClient -Verbose VERBOSE: This machine's DNS records will be updated on the DNS server. This may affect the results of DNS queries for this machine.
Get-DnsClientCache replaces ipconfig /displaydns
When a hostname doesn’t appear to be resolving to the correct IP address or doesn’t resolve at all, you could troubleshoot with ipconfig /displaydns. It will show you a full list of resolved DNS records currently in the client cache. It will likely be a long list, and can be time consuming to scroll through.
PowerShell’s Get-DnsClientCache cmdlet enables more control for filtering DNS cache results. Running Get-DNSClientCache by itself will return a full list in table format.
PS C:\Users\aaron> Get-DnsClientCache Entry RecordName Record Status Section TimeTo Data Data Type Live Length ----- ---------- ------ ------ ------- ------ ------ ---- id.google.com id.google.com CNAME Success Answer 19 8 id.l.google.com id.google.com id.l.google.com A Success Answer 19 4 220.127.116.11 thinkpowershell.com thinkpowershell.com A Success Answer 2559 4 18.104.22.168 technet.microsoft.com technet.microsoft.com CNAME Success Answer 120 8 technet.microsoft.akadns.net ...
You can filter with a number of different cmdlet parameters. For example, if I am investigating the DNS resolution for thinkpowershell.com, I can return only that record using the -Name parameter (wild cards ARE supported). Refer to the REFERENCE section for a TechNet link with the full parameter list.
PS C:\Users\aaron> Get-DnsClientCache -Name "*thinkpowershell*" Entry RecordName Record Status Section TimeTo Data Data Type Live Length ----- ---------- ------ ------ ------- ------ ------ ---- thinkpowershell.com thinkpowershell.com A Success Answer 2616 4 22.214.171.124
Clear-DnsClientCache replaces ipconfig /flushdns
My domain thinkpowershell.com has a long TTL, however let’s say the IP has changed and I know the DNS server responsible for the domain has been updated. I can force my computer to clear its DNS cache and re-query DNS by using the cmdlet Clear-DnsClientCache. It accomplishes the same task as ipconfig /flushdns. You can see the DNS client cache now has no record for thinkpowershell.com.
PS C:\Users\aaron> Get-DnsClientCache -Name "*thinkpowershell*" Entry RecordName Record Status Section TimeTo Data Data Type Live Length ----- ---------- ------ ------ ------- ------ ------ ---- thinkpowershell.com thinkpowershell.com A Success Answer 2616 4 126.96.36.199 PS C:\Users\aaron> Clear-DnsClientCache PS C:\Users\aaron> Get-DnsClientCache -Name "*thinkpowershell*" PS C:\Users\aaron>
Advantage over ipconfig: With CimSession you can run these cmdlets on remote computers
The killer advantage of all these cmdlets is they have a -CimSession parameter, which means with the correct configuration and permissions, you can use these cmdlets to perform these same actions against remote computers. I won’t get into the details of CimSessions (and CIM in general and how awesome it is) in this post, but make a mental note.
- In a lab environment with a Windows DNS server, delete the DNS A record of a lab workstation. On the lab workstation, run Register-DnsClient and verify a new A record is created.
- Run Get-DnsClientCache with a parameter to return a filtered set of cached DNS records.
- Run Clear-DnsClientCache and verify the cache is now empty.