Automating the world one-liner at a time…
<Edited 7/2/2006 to add tags and category>Wei Wu provided a nice one liner in response to a query in our NewsGroup: Microsoft.Public.Windows.PowerShell about how to resolve a hostname into an IP Address:
PS> [System.Net.Dns]::GetHostAddresses("www.msn.com")
IPAddressToString : 207.68.173.76Address : 1286423759AddressFamily : InterNetworkScopeId :IsIPv6Multicast : FalseIsIPv6LinkLocal : FalseIsIPv6SiteLocal : False
If you want to go from address to name:
PS> [System.Net.Dns]::GetHostbyAddress("207.46.198.30")
HostName Aliases AddressList-------- ------- -----------wwwtkttest2.microsoft.com {} {207.46.198.30}
Jeffrey SnoverWindows PowerShell Architect
PSMDTAG:FAQ: How do I resolve a hostname?PSMDTAG:DOTNET: GetHostByAddress
Is there a way to create a DNS record (any type) using .net? I realize you can use WMI and the MicrosoftDNS namespace to do this however i am wanting to try out some .net via powershell and can't find a way to do it - is this possible?
I can see from this blog that retrieving DNS information is not a problem - what about writing to DNS???
Thanks in advance
PingBack from http://dmitrysotnikov.wordpress.com/2008/03/07/get-computer-by-ip-address/
> [System.Net.Dns]::GetHostAddresses("www.msn.com")
From the perspective of a System Admin, this is not a great answer. Having to track through .Net libraries to find how to get an IP suggests PowerShell thinks like a developer, and not like a command line.
Too much typing just to get an IP; too much tracking through .Net libraries to find "how." Admins don't just look up IPs, they have look up other types of records (NS, MX), track down why things don't resolve correctly by hitting other DNS servers....
Why couldn't PowerShell just make an object oriented port of NSLookup? What's next: we have to manually create ICMP packets and monitor for a response?
Just what I needed - Powershell rocks!
Still, for a seasoned Powershell user to reach the next level, a deeper understanding of the .net fw is necessary. So, this kind of post is priceless.
Thank You so much.
PingBack from http://www.fuzzlinks.com/2008/07/14/powershell-team-blog-windows-powershell-one-liner-name-to-ip-address/
Does this work on all DNS names?
Great article, I was able to use it and with a minor modification do a quick reverse lookup so that we could translate a list of IPs into their respective hostname.
Big problem is that this .net class will check local caches and netbios. You are not garunteed to be getting back a actual DNS record from a DNS server.
It will also return the IPv4 and IPv6 addresses for all enabled adapters if running on a local machine.
OK...I get the following result:
Address : 454113089
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IPAddressToString : 65.55.17.27
What I want to do is use the IP address in another part of the script. I would expect that I could use "IPAddressToString" as a property as so: "$IP.IPAddressToString" but when I do that it comes back as a null value. How do I reference the IP address in the rest of the script after identifying it?
([System.Net.Dns]::GetHostaddresses("hostname".split('.')[0]))[0].ipaddresstostring
$hn = "hostname"
$ip = [System.Net.Dns]::GetHostAddresses($hn) | select-object IPAddressToString -expandproperty IPAddressToString