-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-DNSHostEntryAsync.ps1
88 lines (82 loc) · 2.61 KB
/
Get-DNSHostEntryAsync.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
.EXAMPLE
Get-DNSHostEntryAsync -ComputerName 8.8.8.8, ibm.com
Would return:
ComputerName Result
------------ ------
8.8.8.8 dns.google
ibm.com 129.42.38.10
.OUTPUTS
Net.AsyncGetHostResult
.NOTES
Inspired by: Get-DNSHostEntryAsync by Boe Prox
Changes:
* added example to comment based help
* slight formatting changes
#>
#Requires -Version 3.0
[OutputType('Net.AsyncGetHostResult')]
[CmdletBinding(ConfirmImpact='None')]
Param (
[parameter(ValueFromPipeline)]
[Alias('CN', 'Server')]
[string[]] $ComputerName
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
$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 { Write-Error -Message 'Error encountered'}
$Task | ForEach-Object {
$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
}
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}