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 hidden or 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
- Pipeline Input
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment