Public/Test-ComputerPing.ps1

<#
.SYNOPSIS
    Checks the ping status of a computer.
 
.DESCRIPTION
    This function checks the ping status of a computer specified by the input parameter.
 
.PARAMETER Computer
    Specifies the name or IP address of the computer to ping.
 
.EXAMPLE
    Test-ComputerPing -Computer "192.168.1.1"
    Checks the ping status of the computer with the IP address 192.168.1.1.
 
.EXAMPLE
    "192.168.1.1" | Test-ComputerPing
    Checks the ping status of the computer with the IP address 192.168.1.1 using pipeline input.
 
.NOTES
    Author: Sundeep Eswarawaka
#>

function Test-ComputerPing {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$Computer
    )

    Begin {
        $ErrorActionPreference = "SilentlyContinue"
        Write-Verbose "Pinging $Computer..."
    }

    Process {
        try {
            $pingResult = Get-WmiObject win32_pingstatus -Filter "address='$Computer'"
            $resultCode = $pingResult.StatusCode
            if ($resultCode -eq 0) {
                Write-Output "Ping successful on $Computer"
            } else {
                Write-Output "$Computer is offline or not responding to ping"
            }
        } catch {
            Write-Output "Ping failed with error: $_ on $Computer"
        }
    }

    End {
        # Remove Variables
        Remove-Variable -Name Computer -ErrorAction SilentlyContinue
    }
}