Test-PSOnePing.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

function Test-PSOnePing
{
  <#
    .SYNOPSIS
    Sends a ping (ICMP) to a computer
 
    .DESCRIPTION
    Sends a ping (ICMP) to a computer
 
    .EXAMPLE
    Test-PSOnePing -ComputerName 127.0.0.1, microsoft.com, powershell.one -Timeout 2000
    Pings three computers with a maximum timeout of 2000 milliseconds
 
    .EXAMPLE
    '127.0.0.1', 'microsoft.com', 'powershell.one' | Test-PSOnePing -Timeout 2000
    Pings three computers with a maximum timeout of 2000 milliseconds
 
    .EXAMPLE
    Get-ADComputer -Filter * | Select-Object -ExpandProperty Name | Test-PSOnePing -Timeout 2000
    Pings all computers received from Get-ADComputer with a maximum timeout of 2000 milliseconds
    Module "ActiveDirectory" required for Get-ADComputer
 
    .LINK
    https://powershell.one/tricks/network/ping
  #>



  param
  (
    # Computername or IP address to ping
    [Parameter(Mandatory,ValueFromPipeline)]
    [string[]]
    $ComputerName,
    
    # Timeout in milliseconds
    [int]
    [ValidateRange(100,50000)]
    $Timeout = 2000
  )
  
  begin
  {
    $Online = @{
      Name = 'Online'
      Expression = { $_.Status -eq 'Success' }
    }
    $obj = New-Object System.Net.NetworkInformation.Ping
  }
  
  process
  {
    $ComputerName | 
    ForEach-Object {
      $obj.Send($_, $timeout) |
      Select-Object -Property $Online, Status, Address |
      Add-Member -MemberType NoteProperty -Name Name -Value $_ -PassThru
    }
  }
}