Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/4cd4692864c28f7f4d82 to your computer and use it in GitHub Desktop.
Save guitarrapc/4cd4692864c28f7f4d82 to your computer and use it in GitHub Desktop.
Resolve HostName to IPAddress / IPAddress to HostName
#Requires -Version 3.0
<#
.Synopsis
Get HostName to IPAddress Entry / IPAddress to HostName Entry
.DESCRIPTION
using Dns.GetHostEntryAsync Method.
You can skip Exception for none exist HostNameOrAddress result by adding -SkipException $true
.EXAMPLE
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "neue.cc"
# Test Success
.EXAMPLE
"google.com", "173.194.38.100", "neue.cc" | Get-HostEntryAsync
# Pipeline Input
.EXAMPLE
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "hogemopge.fugapiyo"
# Error will stop execution
.EXAMPLE
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "hogemopge.fugapiyo" -SkipException $true
# Skip Error result
.LINK
http://msdn.microsoft.com/en-US/library/system.net.dns.gethostentryasync(v=vs.110).aspx
#>
function Get-HostEntryAsync
{
[CmdletBinding()]
param
(
[parameter(
Mandatory = 1,
Position = 0,
ValueFromPipeline = 1,
ValueFromPipelineByPropertyName = 1)]
[string[]]
$HostNameOrAddress,
[parameter(
Mandatory = 0,
Position = 1,
ValueFromPipelineByPropertyName = 1)]
[bool]
$SkipException = $false
)
process
{
foreach ($name in $HostNameOrAddress)
{
$x = [System.Net.DNS]::GetHostEntryAsync($name)
$x.ConfigureAwait($false) > $null
$task = [PSCustomObject]@{
HostNameOrAddress = $name
Task = $x
}
$tasks.Add($task)
}
}
end
{
try
{
[System.Threading.Tasks.Task]::WaitAll($tasks.Task)
}
catch
{
$stackStrace = $_
$throw = $Tasks `
| where {$_.Task.Exception} `
| %{
$stackStrace
[System.Environment]::NewLine
"Error HostNameOrAddress : {0}" -f $_.HostNameOrAddress
[System.Environment]::NewLine
$_.Task.Exception
}
if (-not $SkipException)
{
throw $throw
}
else
{
Write-Verbose ("-SkipException was {0}. Skipping Error : '{1}'." -f $SkipException, "$(($Tasks | where {$_.Task.Exception}).HostNameOrAddress -join ', ')")
}
}
finally
{
foreach ($task in $tasks.Task)
{
[System.Net.IPHostEntry]$IPHostEntry = $task.Result
$IPHostEntry
}
}
}
begin
{
$tasks = New-Object 'System.Collections.Generic.List[PSCustomObject]'
}
}
@guitarrapc
Copy link
Author

  • Success Result
# Test Success
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "neue.cc"
HostName                          Aliases                           AddressList                     
--------                          -------                           -----------                     
google.com                        {}                                {173.194.38.100, 173.194.38.1...
nrt19s18-in-f4.1e100.net          {}                                {173.194.38.100}                
neue.cc                           {}                                {202.181.97.92}                 

@guitarrapc
Copy link
Author

  • Fail Result
# Test Error
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "hogemopge.fugapiyo"
Exception calling "WaitAll" with "1" argument(s): "One or more errors occurred." 
 Error HostNameOrAddress : hogemopge.fugapiyo 
 System.AggregateException: One or more errors occurred. ---> System.Net.Sockets.SocketException: 
No such host is known
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, 
Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, 
Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<---
At line:43 char:13
+             throw $Tasks `
+             ~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (System.Object[]:Object[]) [], RuntimeException
    + FullyQualifiedErrorId : Exception calling "WaitAll" with "1" argument(s): "One or more errors 
    occurred." 
 Error HostNameOrAddress : hogemopge.fugapiyo 
     System.AggregateException: One or more errors occurred. ---> System.Net.Sockets.SocketExceptio 
   n: No such host is known
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunct 
   ion, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.Net.Sockets.SocketException (0x80004005): No such host is know 
   n
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunct 
   ion, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<---

@guitarrapc
Copy link
Author

  • SkipException $True
# Test Error with Skip Exception
Get-HostEntryAsync -HostNameOrAddress "google.com", "173.194.38.100", "hogemopge.fugapiyo" -SkipException $true
HostName                          Aliases                           AddressList                     
--------                          -------                           -----------                     
google.com                        {}                                {173.194.126.160, 173.194.126...
nrt19s18-in-f4.1e100.net          {}                                {173.194.38.100}                

@guitarrapc
Copy link
Author

  • Pipeline Input
# Pipeline Input
"google.com", "173.194.38.100", "neue.cc" | Get-HostEntryAsync
HostName                          Aliases                           AddressList                     
--------                          -------                           -----------                     
google.com                        {}                                {173.194.117.162, 173.194.117...
nrt19s18-in-f4.1e100.net          {}                                {173.194.38.100}                
neue.cc                           {}                                {202.181.97.92}                 

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