Last active
August 29, 2015 14:04
-
-
Save guitarrapc/4cd4692864c28f7f4d82 to your computer and use it in GitHub Desktop.
Resolve HostName to IPAddress / IPAddress to HostName
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]' | |
} | |
} |
Author
guitarrapc
commented
Jul 30, 2014
- Success Result
- 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)<---
- 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}
- 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