Replaced Windows registry lookup with WMIC process invocation #115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reading the registry is a weak solution because we don't know if the settings we're reading are from active interfaces or not. WMI, by contrast, only emits the
DNSServerSearchOrder
key (containing the DNS name servers) when the interface is actively in use (connected).Drawbacks
WMI is still not a perfect solution because if there are multiple active interfaces (uncommon), we don't know which interface or DNS servers Windows will actually use, but generally this is unimportant because (1) usually there's only one active interface, and (2) where there are multiple in use, if the name servers are public anyway the probability that any will work is high. Where this might be a problem is when using a VPN or other tunnel that would be unreachable from a different NIC. It is supposed there are no Windows users currently experiencing that use-case, however if there are, I do not have a solution for them.
Deprecated approach
WMIC as an interface for WMI is actually deprecated (lol). Nevertheless, it's still available by default in Windows 11 at this time, but the "next version of Windows" will have it disabled by default. It is unclear to me if that means Windows 12 or the next service pack for Windows 11. In any case, we should look to move towards PowerShell in the near future, which seems to be supported OOTB since Windows 7 (albeit with a diminished feature set).
PowerShell
One benefit of using PowerShell is we won't need to do any output massaging since it can be tamed using various tools embedded in the shell itself. The following invocation will emit the name servers one per line.
However, PowerShell is notably slower than WMIC. A typical benchmark on my system yields the following results.
That is, PowerShell takes almost twice as long to output the same information. Most of this time is overhead from shell start-up, though; once in a PowerShell session, the command may execute very quickly, but this is a moot point for our use case.
IPv6
One other observed characteristic of the
DNSServerSearchOrder
key we're querying here is that it never seems to contain IPv6 addresses. Other properties ofWin32_NetworkAdapterConfiguration
do contain references to IPv6 addresses, such asIPAddress
andDefaultIPGateway
, so WMI isn't IPv6-phobic itself, thus leading to a preponderance of the perpetual absence in the DNS space. There is no solution known to me at this time to retrieve the IPv6 servers by any WMI mechanism, whether we migrate to PowerShell or not.References