Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Created October 24, 2015 12:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PrateekKumarSingh/586f2d3d43f7e8cb07ce to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/586f2d3d43f7e8cb07ce to your computer and use it in GitHub Desktop.
Function Resolve-Host()
{
Param(
[Parameter(Mandatory=$true,Position=0)] $HostEntry,
[Switch] $HostnameToIP,
[Switch] $FlushDNS
)
If($FlushDNS)
{
Ipconfig /FlushDNS | Out-Null
}
If($HostnameToIP)
{
$object = @()
$HostEntry | %{
$Object += New-Object psobject -Property @{ HostName= $_
IPAddress=$([System.Net.Dns]::gethostentry($_).AddressList.IPAddressToString)
}
}
Return $object | select Hostname, IPAddress
}
else
{
$object = @()
$HostEntry | %{
$Object += New-Object psobject -Property @{ IPAddress= $_
HostName=$([System.Net.Dns]::gethostentry($_).HostName)
}
}
Return $object | select IPAddress, Hostname
}
}
Copy link

ghost commented Apr 21, 2017

Thanks, very useful function! I slightly refactored it to look a bit more pretty! resolve-host.ps1
I'm subscribed to your blog, the stuff you are doing with Powershell is very exciting!

@zookato
Copy link

zookato commented Sep 8, 2020

#Must First run
. .\Resolve-Host.Ps1
there is space after the first dot

#Then
Resolve-Host "1.1.1.1","8.8.8.8"
Resolve-Host "bing.com" -HostnameToIP
Resolve-Host (gc .\IPAddresses.txt) | FT -AutoSize
Resolve-Host (gc .\IPAddresses.txt) -HostnameToIP | FT -AutoSize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment