3

I see people using this:

[Net.DNS]::GetHostEntry("serverName").addressList[0].IPAddressToString

...and that will work for me. Except, what if I have an entry in the hosts file? Does this use the same method that Windows uses or skip the DNS cache and hosts file and always query a DNS server? I would like to use whatever method Windows uses for browers, Windows Explorer, ping, etc.

2
  • 1
    So what happens if you add a dummy entry for, say, google.com to your hosts file and the run [Net.DNS]::GetHostEntry("google.com").addressList[0].IPAddressToString? Commented Feb 14, 2018 at 3:09
  • Why not give it a try and see what happens? (It will be faster than waiting for someone else to test it for you and write a response.) Commented Feb 14, 2018 at 15:13

1 Answer 1

5

Why not just use the built-in DNS cmdlets? Or is there a particular reason you are traveling down the raw .Net path? Code project, homework assignment, curiosity?

 Get-Command -Name Resolve-Dns* | Format-Table -AutoSize

CommandType Name                   Version Source       
----------- ----                   ------- ------       
Cmdlet      Resolve-DnsName        1.0.0.0 DnsClient     



# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Resolve-DnsName).Parameters
Get-help -Name Resolve-DnsName -Examples
Get-help -Name Resolve-DnsName -Full
Get-help -Name Resolve-DnsName -Online


Resolve-DnsName -Name stackoverflow.com -Type A | Format-Table -AutoSize

Name              Type TTL Section IPAddress     
----              ---- --- ------- ---------     
stackoverflow.com A    268 Answer  151.101.65.69 
stackoverflow.com A    268 Answer  151.101.193.69
stackoverflow.com A    268 Answer  151.101.129.69
stackoverflow.com A    268 Answer  151.101.1.69  



Get-Content C:\WINDOWS\System32\Drivers\Etc\hosts

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost


Resolve-DnsName -Name localhost -Type ANY | Format-Table -AutoSize


Name              Type TTL  Section IPAddress
----              ---- ---  ------- ---------
localhost.lab.net A    1260 Answer  127.0.0.1



Resolve-DnsName -Name $env:COMPUTERNAME -Type ANY | Format-Table -AutoSize

Name Type TTL Section IPAddress                             
---- ---- --- ------- ---------                             
WS01 A    30  Answer  10....                            
WS01 AAAA 30  Answer  2601:...             
WS01 AAAA 30  Answer  2601:...
WS01 AAAA 30  Answer  fe80::...  

Using the raw .Net call, will give you ...

[Net.DNS]::GetHostEntry('stackoverflow.com').addressList[0].IPAddressToString
151.101.65.69

[Net.DNS]::GetHostEntry('localhost').addressList[0].IPAddressToString
::1

[Net.DNS]::GetHostEntry($env:COMPUTERNAME).addressList[0].IPAddressToString
fe80::...
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't thinking clearly. I should have done more testing myself. And using the .Net commands usually gives more options that are sometimes more precisely what I need, plus there's more likely to be a method I can use that is more likely to be found with a Google search and more likely to be available on more servers regardless of operating system version. And, once I know the command, it's not really any more difficult to use than a cmdlet. Thanks.
for powershell, what's the distinction between a cmdlet and .NET command?
This is giving me unknown host when i try getting hostname for IP Address...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.