SendNetworkPing.psm1


#
#
# Send Network Ping Module
#
#


$ErrorActionPreference = 'Stop'


#
# Module functions
#


Function Send-NetworkPing {

    <#
 
     .SYNOPSIS
     Send a ping to a computer
 
     .DESCRIPTION
     Send a ping to a computer
 
     .PARAMETER ComputerName
     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 -ComputerName www.powershellgallery.com
 
     .EXAMPLE
     Send-NetworkPing -ComputerName LabPC2064 -Quiet
 
     .EXAMPLE
     Send-NetworkPing -ComputerName LabPC2064 -Quiet -TimeOut 200
 
     .NOTES
     N/A
 
     .LINK
     N/A
 
    #>


    Param (

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

        [String[]]$ComputerName,

        [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]@{

                ComputerName = $Values[0]
                Status = $Values[1].ToString()
                IPAddress = $Values[2]

            }

        }

    }

    PROCESS {

        ForEach ($Computer In $ComputerName) {

            Try {

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

                If ($Quiet) {

                    Write-Output $True

                }

                Else {

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

                    $PingReply = $Null

                }

            }

            Catch {

                If ($Quiet) {

                    Write-Output $False

                }

                Else {

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

                    $PingReply = $Null

                }

            }

        }

    }

    END {}

}