SendNetworkPing.psm1


#
#
# Send Network Ping Module
#
#


$ErrorActionPreference = 'Stop'


#
# Module functions
#


Function Send-NetworkPing {

    <#
 
     .SYNOPSIS
     Send a ping to a destination
 
     .DESCRIPTION
     Send a ping to a destination
 
     .PARAMETER Destination
     The target to ping
 
     .PARAMETER TimeOut
     The time in milliseconds to wait for a response
 
     .PARAMETER Quiet
     Switch to omit output (just receive a TRUE or FALSE response)
 
     .EXAMPLE
     Send-NetworkPing -Destination www.powershellgallery.com
 
     .EXAMPLE
     Send-NetworkPing -Destination LabPC2064 -Quiet
 
     .EXAMPLE
     Send-NetworkPing -Destination LabPC2064 -Quiet -TimeOut 200
 
     .NOTES
     N/A
 
     .LINK
     N/A
 
    #>


    Param (

        [Parameter(Mandatory = $True,
                   ValueFromPipeline = $True,
                   ValueFromPipelineByPropertyName = $True,
                   HelpMessage = 'Enter destination'
                  )
        ]

        [String[]]$Destination,

        [Parameter(Mandatory = $False,
                   ValueFromPipeline = $True,
                   ValueFromPipelineByPropertyName = $True,
                   HelpMessage = 'Enter timeout'
                  )
        ]

        [Int]$TimeOut = 100,

        [Switch]$Quiet

    )

    BEGIN {

        $PingObj = New-Object System.Net.NetworkInformation.Ping

        Function Show-Output ($Values) {

            If ($Null -eq $Values[1]) {

                $Values[1] = 'Unreachable'

            }

            [PSCustomObject]@{

                Destination = $Values[0]
                Status = $Values[1]
                IPAddress = $Values[2]

            }

        }

    }

    PROCESS {

        ForEach ($Ping In $Destination) {

            Try {

                $PingReply = $PingObj.Send($Ping, $TimeOut)

                If ($Quiet) {

                    Write-Output $True

                }

                Else {

                    Show-Output ($Ping.ToUpper(), $PingReply.Status, $PingReply.Address)

                    $PingReply = $Null

                }

            }

            Catch {

                If ($Quiet) {

                    Write-Output $False

                }

                Else {

                    Show-Output($Ping.ToUpper(), $PingReply.Status, $PingReply.Address)

                    $PingReply = $Null

                }

            }

        }

    }

    END {}

}