Thursday, May 28, 2015

#74 : How to get IP address from Host Name using Powershell?

There are multiple ways to get it. Some will suggest using ping.exe also. But below is the tested and clean approach to do it.

Replace your host name below and test it at your end :

function GetIPAddress([string] $HostName) 
{
 trap {
  return 0 
 }
 $IP_ADDRESS=$([System.Net.Dns]::GetHostAddresses("$HostName")).IPAddressToString
 return $IP_ADDRESS 

} 

echo $(GetIPAddress "ABCHOSTNAME")


Hope you like this tip, do write your comments and suggestions.
Life is good!

1 comment:

  1. function GetIPAddress([string] $HostName)
    {
    (resolve-hostname $hostname).ipaddress
    }

    seems a little easier

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...