├── LICENSE ├── README.md ├── Get-DNSHostEntryAsync.ps1 └── Test-ConnectionAsync.ps1 /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Boe Prox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #####TestConnectionASync 2 | 3 | Ping all hosts on a designated subnet (limited to /24) 4 | 5 | Without a parameter the whole local subnet is pinged and results are returned. It is super quick . . . 6 | 7 | ``` 8 | PS C:\Windows\system32> Measure-Command {Ping-Subnet} 9 | 10 | Days : 0 11 | Hours : 0 12 | Minutes : 0 13 | Seconds : 3 14 | Milliseconds : 679 15 | Ticks : 36795428 16 | TotalDays : 4.25873009259259E-05 17 | TotalHours : 0.00102209522222222 18 | TotalMinutes : 0.0613257133333333 19 | TotalSeconds : 3.6795428 20 | TotalMilliseconds : 3679.5428 21 | 22 | .EXAMPLE 23 | PS OneDrive:\> Ping-Subnet 24 | Computername Result 25 | ------------ ------ 26 | 192.168.192.1 Success 27 | 192.168.192.17 Success 28 | 192.168.192.20 Success 29 | 192.168.192.23 Success 30 | 192.168.192.28 Success 31 | .EXAMPLE 32 | This command pings all hosts in the designated subnet 33 | 34 | PS OneDrive:\> Ping-Subnet -subnet 212.58.244.0 35 | Computername Result 36 | ------------ ------ 37 | 212.58.244.1 Success 38 | 212.58.244.3 Success 39 | 212.58.244.4 Success 40 | 212.58.244.5 Success 41 | 212.58.244.11 Success 42 | .EXAMPLE 43 | This command reports the hosts on the given subnet which failed to respond to ping 44 | 45 | PS OneDrive:\> Ping-Subnet -subnet 212.58.244.22 -result TimedOut 46 | Computername Result 47 | ------------ ------ 48 | 212.58.244.2 TimedOut 49 | 212.58.244.6 TimedOut 50 | 212.58.244.7 TimedOut 51 | ``` 52 | 53 | #####Get-DNSHostEntryAsync 54 | ``` 55 | .SYNOPSIS 56 | Performs a DNS Get Host asynchronously 57 | .DESCRIPTION 58 | Performs a DNS Get Host asynchronously 59 | .PARAMETER Computername 60 | List of computers to check Get Host against 61 | .NOTES 62 | Name: Get-DNSHostEntryAsync 63 | Author: Boe Prox 64 | Version History: 65 | 1.0 //Boe Prox - 12/24/2015 66 | - Initial result 67 | .OUTPUT 68 | Net.AsyncGetHostResult 69 | .EXAMPLE 70 | Get-DNSHostEntryAsync -Computername google.com,prox-hyperv,bing.com, github.com, powershellgallery.com, powershell.org 71 | Computername Result 72 | ------------ ------ 73 | google.com 216.58.218.142 74 | prox-hyperv 192.168.1.116 75 | bing.com 204.79.197.200 76 | github.com 192.30.252.121 77 | powershellgallery.com 191.234.42.116 78 | powershell.org {104.28.15.25, 104.28.14.25} 79 | .EXAMPLE 80 | Get-DNSHostEntryAsync -Computername 216.58.218.142 81 | Computername Result 82 | ------------ ------ 83 | 216.58.218.142 dfw25s08-in-f142.1e100.net 84 | ``` 85 | -------------------------------------------------------------------------------- /Get-DNSHostEntryAsync.ps1: -------------------------------------------------------------------------------- 1 | Function Get-DNSHostEntryAsync { 2 | <# 3 | .SYNOPSIS 4 | Performs a DNS Get Host asynchronously 5 | 6 | .DESCRIPTION 7 | Performs a DNS Get Host asynchronously 8 | 9 | .PARAMETER Computername 10 | List of computers to check Get Host against 11 | 12 | .NOTES 13 | Name: Get-DNSHostEntryAsync 14 | Author: Boe Prox 15 | Version History: 16 | 1.0 //Boe Prox - 12/24/2015 17 | - Initial result 18 | 19 | .OUTPUT 20 | Net.AsyncGetHostResult 21 | 22 | .EXAMPLE 23 | Get-DNSHostEntryAsync -Computername google.com,prox-hyperv,bing.com, github.com, powershellgallery.com, powershell.org 24 | 25 | Computername Result 26 | ------------ ------ 27 | google.com 216.58.218.142 28 | prox-hyperv 192.168.1.116 29 | bing.com 204.79.197.200 30 | github.com 192.30.252.121 31 | powershellgallery.com 191.234.42.116 32 | powershell.org {104.28.15.25, 104.28.14.25} 33 | 34 | .EXAMPLE 35 | Get-DNSHostEntryAsync -Computername 216.58.218.142 36 | 37 | Computername Result 38 | ------------ ------ 39 | 216.58.218.142 dfw25s08-in-f142.1e100.net 40 | #> 41 | #Requires -Version 3.0 42 | [OutputType('Net.AsyncGetHostResult')] 43 | [cmdletbinding()] 44 | Param ( 45 | [parameter(ValueFromPipeline=$True)] 46 | [string[]]$Computername 47 | ) 48 | Begin { 49 | $Computerlist = New-Object System.Collections.ArrayList 50 | If ($PSBoundParameters.ContainsKey('Computername')) { 51 | [void]$Computerlist.AddRange($Computername) 52 | } Else { 53 | $IsPipeline = $True 54 | } 55 | } 56 | Process { 57 | If ($IsPipeline) { 58 | [void]$Computerlist.Add($Computername) 59 | } 60 | } 61 | End { 62 | $Task = ForEach ($Computer in $Computername) { 63 | If (([bool]($Computer -as [ipaddress]))) { 64 | [pscustomobject] @{ 65 | Computername = $Computer 66 | Task = [system.net.dns]::GetHostEntryAsync($Computer) 67 | } 68 | } Else { 69 | [pscustomobject] @{ 70 | Computername = $Computer 71 | Task = [system.net.dns]::GetHostAddressesAsync($Computer) 72 | } 73 | } 74 | } 75 | Try { 76 | [void][Threading.Tasks.Task]::WaitAll($Task.Task) 77 | } Catch {} 78 | $Task | ForEach { 79 | $Result = If ($_.Task.IsFaulted) { 80 | $_.Task.Exception.InnerException.Message 81 | } Else { 82 | If ($_.Task.Result.IPAddressToString) { 83 | $_.Task.Result.IPAddressToString 84 | } Else { 85 | $_.Task.Result.HostName 86 | } 87 | } 88 | $Object = [pscustomobject]@{ 89 | Computername = $_.Computername 90 | Result = $Result 91 | } 92 | $Object.pstypenames.insert(0,'Net.AsyncGetHostResult') 93 | $Object 94 | } 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /Test-ConnectionAsync.ps1: -------------------------------------------------------------------------------- 1 | Function Ping-Subnet{ 2 | <# 3 | .SYNOPSIS 4 | Ping an entire subnet quickly (asynchronously) 5 | .DESCRIPTION 6 | Uses TestConnectionAsync module from Boe Prox (Msft) 7 | 8 | .EXAMPLE 9 | This command performs an asynchronous ping on all hosts within your subnet 10 | Running the command without specifying a subnet will only work on Windows 8 or higher as it uses the Get-NetIPConfiguration cmdlet 11 | 12 | PS OneDrive:\> Ping-Subnet 13 | Computername Result 14 | ------------ ------ 15 | 192.168.192.1 Success 16 | 192.168.192.17 Success 17 | 192.168.192.20 Success 18 | 192.168.192.23 Success 19 | 192.168.192.28 Success 20 | .EXAMPLE 21 | This command pings all hosts in the designated subnet 22 | 23 | PS OneDrive:\> Ping-Subnet -subnet 212.58.244.0 24 | Computername Result 25 | ------------ ------ 26 | 212.58.244.1 Success 27 | 212.58.244.3 Success 28 | 212.58.244.4 Success 29 | 212.58.244.5 Success 30 | 212.58.244.11 Success 31 | .EXAMPLE 32 | This command reports the hosts on the given subnet which failed to respond to ping 33 | 34 | PS OneDrive:\> Ping-Subnet -subnet 212.58.244.22 -result TimedOut 35 | Computername Result 36 | ------------ ------ 37 | 212.58.244.2 TimedOut 38 | 212.58.244.6 TimedOut 39 | 212.58.244.7 TimedOut 40 | .EXAMPLE 41 | Outputs hosts which responded to ping to a GUI based table 42 | 43 | PS OneDrive:\> Ping-Subnet -subnet 212.58.244.22 -result Success | Out-GridView 44 | .NOTES 45 | # Original Source for Test-ConnectionAsync (don't use PSGallery Version) 46 | # https://github.com/proxb/AsyncFunctions/blob/master/Test-ConnectionAsync.ps1 47 | #> 48 | [cmdletbinding()] 49 | Param( 50 | # Subnet to ping (optional) 51 | [Parameter(ValueFromPipelineByPropertyName=$true, Position=0)] 52 | [ValidateScript({$_ -match [IPAddress]$_ })] 53 | [String]$Subnet, 54 | 55 | # Show Successful or Failed 56 | [ValidateSet('Success', 'TimedOut')] 57 | [String]$Result = 'Success' 58 | ) 59 | 60 | Begin{ 61 | If (-Not($subnet)){ 62 | Write-Verbose "Checking OS Version" 63 | If(([System.Version] (Get-CimInstance -ClassName Win32_OperatingSystem).Version) -ge [System.Version] 6.2){ 64 | Write-Verbose "Checking local subnet" 65 | $myIP = Get-NetIPConfiguration | Select IPv4Address 66 | $octect = $myIP.IPv4Address.IPv4Address -split "\." # backslash is escape char 67 | } 68 | Else{ 69 | Write-Warning "On your version of Windows you need to supply a value for the -Subnet parameter `n 70 | Example: `n 71 | PS C:\> Ping-Subnet -subnet 172.26.75.0" 72 | break 73 | } 74 | } 75 | Else{ 76 | Write-Verbose "Parameter subnet set to $subnet" 77 | $octect = $subnet -split "\." 78 | Write-Verbose "$subnet split into $octect" 79 | } 80 | 81 | } 82 | 83 | Process{ 84 | 85 | $range = for ($i = 1; $i -lt 255; $i += 1){ 86 | [PSCustomObject]@{ 87 | testIP = "$($octect.Item(0)).$($octect.Item(1)).$($octect.Item(2)).$($i)" 88 | } 89 | } 90 | 91 | Write-Verbose "Range to be scanned is $($range.testip)" 92 | Test-ConnectionAsync -Computer $range.testip -TimeToLive 2000 | select computername, result | where result -eq $result 93 | 94 | } 95 | 96 | } 97 | 98 | Function Test-ConnectionAsync { 99 | <# 100 | .SYNOPSIS 101 | Performs a ping test asynchronously 102 | .DESCRIPTION 103 | Performs a ping test asynchronously 104 | .PARAMETER Computername 105 | List of computers to test connection 106 | .PARAMETER Timeout 107 | Timeout in milliseconds 108 | .PARAMETER TimeToLive 109 | Sets a time to live on ping request 110 | .PARAMETER Fragment 111 | Tells whether to fragment the request 112 | .PARAMETER Buffer 113 | Supply a byte buffer in request 114 | .NOTES 115 | Name: Test-ConnectionAsync 116 | Author: Boe Prox 117 | Version History: 118 | 1.0 //Boe Prox - 12/24/2015 119 | - Initial result 120 | .OUTPUT 121 | Net.AsyncPingResult 122 | .EXAMPLE 123 | Test-ConnectionAsync -Computername server1,server2,server3 124 | Computername Result 125 | ------------ ------ 126 | Server1 Success 127 | Server2 TimedOut 128 | Server3 No such host is known 129 | Description 130 | ----------- 131 | Performs asynchronous ping test against listed systems. 132 | #> 133 | #Requires -Version 3.0 134 | [OutputType('Net.AsyncPingResult')] 135 | [cmdletbinding()] 136 | Param ( 137 | [parameter(ValueFromPipeline=$True)] 138 | [string[]]$Computername, 139 | [parameter()] 140 | [int32]$Timeout = 100, 141 | [parameter()] 142 | [Alias('Ttl')] 143 | [int32]$TimeToLive = 128, 144 | [parameter()] 145 | [switch]$Fragment, 146 | [parameter()] 147 | [byte[]]$Buffer 148 | ) 149 | Begin { 150 | 151 | If (-NOT $PSBoundParameters.ContainsKey('Buffer')) { 152 | $Buffer = 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 153 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69 154 | } 155 | $PingOptions = New-Object System.Net.NetworkInformation.PingOptions 156 | $PingOptions.Ttl = $TimeToLive 157 | If (-NOT $PSBoundParameters.ContainsKey('Fragment')) { 158 | $Fragment = $False 159 | } 160 | $PingOptions.DontFragment = $Fragment 161 | $Computerlist = New-Object System.Collections.ArrayList 162 | If ($PSBoundParameters.ContainsKey('Computername')) { 163 | [void]$Computerlist.AddRange($Computername) 164 | } Else { 165 | $IsPipeline = $True 166 | } 167 | } 168 | Process { 169 | If ($IsPipeline) { 170 | [void]$Computerlist.Add($Computername) 171 | } 172 | } 173 | End { 174 | $Task = ForEach ($Computer in $Computername) { 175 | [pscustomobject] @{ 176 | Computername = $Computer 177 | Task = (New-Object System.Net.NetworkInformation.Ping).SendPingAsync($Computer,$Timeout, $Buffer, $PingOptions) 178 | } 179 | } 180 | Try { 181 | [void][Threading.Tasks.Task]::WaitAll($Task.Task) 182 | } Catch {} 183 | $Task | ForEach { 184 | If ($_.Task.IsFaulted) { 185 | $Result = $_.Task.Exception.InnerException.InnerException.Message 186 | $IPAddress = $Null 187 | } Else { 188 | $Result = $_.Task.Result.Status 189 | $IPAddress = $_.task.Result.Address.ToString() 190 | } 191 | $Object = [pscustomobject]@{ 192 | Computername = $_.Computername 193 | IPAddress = $IPAddress 194 | Result = $Result 195 | } 196 | $Object.pstypenames.insert(0,'Net.AsyncPingResult') 197 | $Object 198 | } 199 | } 200 | 201 | } 202 | --------------------------------------------------------------------------------