Functions/Get-DNSHostEntryAsync.ps1

Function Get-DNSHostEntryAsync {
<#
.SYNOPSIS
    Performs a DNS Get Host asynchronously
.DESCRIPTION
    Performs a DNS Get Host asynchronously
.EXAMPLE
    Get-DNSHostEntryAsync -Computername google.com,prox-hyperv,bing.com, github.com, powershellgallery.com, powershell.org
.OUTPUTS
    Net.AsyncGetHostResult
.NOTES
    Name: Get-DNSHostEntryAsync
    Author: Boe Prox
    Version History:
        1.0 - Boe Prox - 12/24/2015
            - Initial result
#>


    #Requires -Version 3.0
    [OutputType('Net.AsyncGetHostResult')]
    [CmdletBinding(ConfirmImpact='None')]
    Param (
        [parameter(ValueFromPipeline)]
        [string[]]$Computername
    )

    begin {
        $Computerlist = New-Object -TypeName System.Collections.ArrayList
        If ($PSBoundParameters.ContainsKey('Computername')) {
            $null = $Computerlist.AddRange($Computername)
        } Else {
            $IsPipeline = $True
        }
    }

    process {
        If ($IsPipeline) {
            $null = $Computerlist.Add($Computername)
        }
    }

    end {
        $Task = ForEach ($Computer in $Computername) {
            If (([bool]($Computer -as [ipaddress]))) {
                [pscustomobject] @{
                    Computername = $Computer
                    Task = [system.net.dns]::GetHostEntryAsync($Computer)
                }
            } Else {
                [pscustomobject] @{
                    Computername = $Computer
                    Task = [system.net.dns]::GetHostAddressesAsync($Computer)
                }
            }
        }
        Try {
            $null = [Threading.Tasks.Task]::WaitAll($Task.Task)
        } Catch {}
        $Task | ForEach {
            $Result = If ($_.Task.IsFaulted) {
                $_.Task.Exception.InnerException.Message
            } Else {
                If ($_.Task.Result.IPAddressToString) {
                    $_.Task.Result.IPAddressToString
                } Else {
                    $_.Task.Result.HostName
                }
            }
            $Object = [pscustomobject]@{
                Computername = $_.Computername
                Result = $Result
            }
            $Object.pstypenames.insert(0,'Net.AsyncGetHostResult')
            $Object
        }
    }

}