none
Display only the value of Lost packets from a ping of default gateway IP using Powershell RRS feed

  • Question

  • Hi Guys, I am barely new with powershell scripting. What I want to happen is that when you ping the default IP Gateway from a client computer, it will write output on the screen with its result depending on the condition met below.

    If default gateway IP is available and packets lost is 0, a message will output on the screen

    If default gateway IP is available and packets lost is not 0, a message will output on the screen

    If default gateway IP is not available or null a message will output on the screen

    Thank you and best regards!

    Thursday, August 3, 2017 4:47 AM

Answers

  • I think i got it

    $pingcount = 4
    $QuitPing = "Check connections again"

    While($QuitPing -ne "Q")
    {
    $serverIP = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
    If ($serverIP -eq $null )
    {
    $QuitPing = Read-Host " The Default Gateway appears to be invalid or missing, please check physical connections. `n Enter R to RETRY or Q to QUIT"
    }
    else
    {
    "Valid Default Gateway IP detected"
    Break
    }
    if($QuitPing -eq "Q")
    {Exit}
    }


    $pingResults = Test-Connection -ComputerName $serverIP -Count $pingcount
    $pingLost = $pingcount - ($pingResults).Count
    $pingResults

    if($pingLost -eq 0)
    {
    Write-Output "`n There are $pingLost packets Lost"
    }
    else
    {
    Write-Output "`n There are $pingLost packets lost, please check with Network administrator or ISP"
    }

    • Marked as answer by Mrpofo Friday, August 4, 2017 6:27 AM
    Friday, August 4, 2017 6:27 AM

All replies

  • Thursday, August 3, 2017 5:15 AM
  • Hi JRV, below is my script. When I test and force default gateway IP with NO IP address assigned, it does NOT output the first "if condition". What could be wrong?#Ping Default Gateway

    Write-Host "Pinging Default Gateway.." -ForegroundColor Green
    $nic = (Get-WmiObject -class win32_NetworkAdapterConfiguration -filter "DHCPEnabled=true AND IPEnabled=true")

    #We will use this to record the number of packets lost. If it remains null, then the ping failed because it could not find the number of lost packets.
    $packetsLost = $null

    #We store results from the ping variable and then go through each line of the results looking for ""Lost" to determine the number of Lost Packets.
    $pingResults = ping $nic
    ping $nic.DefaultIPGateway

    foreach ($line in $pingResults)
    {
        #Check if the current line contains "Lost"
        if ($line.Contains("Lost"))
        {
            #If the line contains "Lost", we need to find the index of it and then take the corresponding sub string which contains the number of packets lost. We can exit the loop
            $packetsLost = ($line.Substring(($line.IndexOf("Lost")+7), "1"))
            Break
        }
    }
    if($packetsLost)
    {
        "There was a problem pinging the Gateway Server. Please check physical connection"
    }
    elseif ($packetsLost = "0")
    {
        "There were no packets lost when pinging the gateway server."
    }
    else
    {
        "There were $packetsLost packets lost when pinging the gateway server. Please check server connection"

    }


    • Edited by Mrpofo Thursday, August 3, 2017 6:15 AM
    Thursday, August 3, 2017 6:14 AM
  • help Test-Connecction -full


    \_(ツ)_/

    Thursday, August 3, 2017 7:15 AM
  • I think i got it

    $pingcount = 4
    $QuitPing = "Check connections again"

    While($QuitPing -ne "Q")
    {
    $serverIP = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
    If ($serverIP -eq $null )
    {
    $QuitPing = Read-Host " The Default Gateway appears to be invalid or missing, please check physical connections. `n Enter R to RETRY or Q to QUIT"
    }
    else
    {
    "Valid Default Gateway IP detected"
    Break
    }
    if($QuitPing -eq "Q")
    {Exit}
    }


    $pingResults = Test-Connection -ComputerName $serverIP -Count $pingcount
    $pingLost = $pingcount - ($pingResults).Count
    $pingResults

    if($pingLost -eq 0)
    {
    Write-Output "`n There are $pingLost packets Lost"
    }
    else
    {
    Write-Output "`n There are $pingLost packets lost, please check with Network administrator or ISP"
    }

    • Marked as answer by Mrpofo Friday, August 4, 2017 6:27 AM
    Friday, August 4, 2017 6:27 AM