Test-NetConnection -Hops

Welcome Forums General PowerShell Q&A Test-NetConnection -Hops

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105131

    Participant
    Points: 0
    Rank: Member

    Hey All, I created the below PowerShell script. What we THINK it's doing, is looping through all 14 hops between us and an ip address. I believe this is a very general statement and we aren't really sure what our results mean. I'm hoping someone can help explain what exactly the Test-NetConnection function does, and what does it do when you use the -Hops parameter. We're using this to help diagnose a connectivity problem between us and a vendor. Please let me know if I can add anymore information to make this easier to diagnose!

    function FUNCTION-NAME {
    	
    	[CmdletBinding()]
    	Param (
    		[int]$Hop
    	)
    	
    	$test = Test-NetConnection *ip address* -Hops $Hop -InformationLevel Quiet
    	
    	If ($test -eq $true) {
    		return $true
    	} else {
    		Write-Host "HOP $hop FAILED FOR IP ADDRESS $($Data.TraceRoute[$hop - 1]) AT $((Get-Date).ToString())" -ForegroundColor Red
    		
    		if (Test-Path -Path "Path to somewhere") {
    			if (Test-Path -Path "Path to somewhere") {
    				# Add content
    				Add-Content -Path "Path to somewhere" -Value "HOP $hop FAILED FOR IP ADDRESS $($Data.TraceRoute[$hop - 1]) AT $(Get-Date)`n`n"
    			} Else {
    				# Add txt file and content
    				New-Item -Path "Path to somewhere" -ItemType File
    				Add-Content -Path "Path to somewhere" -Value "HOP $hop FAILED FOR IP ADDRESS $($Data.TraceRoute[$hop - 1]) AT $(Get-Date)`n`n"
    			}
    		} Else {
    			# Add user folder, txt file and content
    			New-Item -Path "Path to somewhere" -ItemType Directory
    			New-Item -Path "Path to somewhere" -ItemType File
    			Add-Content -Path "Path to somewhere" -Value "HOP $hop FAILED FOR IP ADDRESS $($Data.TraceRoute[$hop - 1]) AT $(Get-Date)`n`n"
    		}
    		return $false
    	}
    }
    
    
    $count = 1
    $Data = Test-NetConnection *ip address* -TraceRoute
    
    Do {
    	For ($hop = 1; $hop -lt 15; $hop++) {
    		$Result = FUNCTION-NAME -Hop $hop
    		
    		If ($Result -eq $false) {
    			Write-Host "Attempt $count has a failure! It has been logged under Path to somewhere" -ForegroundColor Red
    		}
    	}
    	
    	Write-Host	"Attempt $count completed" -ForegroundColor Green
    	
    	$count++
    } while ($True)
    

    The logging does show that there are failures at every hop along the way, and this is intermittent. But we want to better understand what these results mean, so thank you in advance! I have obviously removed any sensitive information. The below are two lines of text which are examples of what a failure will return. Again, we have seen it fail at every hop at least once.

    HOP 10 FAILED FOR IP ADDRESS *ip address* AT 07/24/2018 17:01:20
    HOP 9 FAILED FOR IP ADDRESS *ip address* AT 07/24/2018 17:01:27
    
  • #105146

    Participant
    Points: 217
    Helping Hand
    Rank: Participant

    Are you saying that the PS built-in help files are not giving you the details you need to understand this?
    What you are asking for, relative to the understanding of hop errors would be the same as asking this tracert from the command line. Yet, the -hops in the command is you specifically telling it how many hops you'd expect, vs not using it and letting it do its thing until all is done or it flat out fails at any given hop.

    So, out of curiosity, have you used tracert to see if your hop info is different?
    tracert has a hops switch as well.

    All these things are just calling libraries in Windows. The below is an example of this calling activity.
    https://www.codeproject.com/articles/14567/tracert-component-for-net-framework

    You can also look at the PS source code for this on the MS opensource PS GitHub repo, where you can see that the -hops param is just this...

        
    [Parameter(ParameterSetName='ICMP')]
    [ValidateRange(1, 120)]
    [int]
    ${Hops}
    

    And that -TraceRoute switch is just running tracert under the covers, as noted in the help files.

        
    -TraceRoute 
    Indicates that Tracert runs to test connectivity to the remote host.
    

    So, what exactly are you looking for this to tell you other than what it is?

  • #105187

    Participant
    Points: 0
    Rank: Member

    Hey postanote, thanks for your reply.

    It does appear that we were misunderstanding what the -Hops parameter actually did. It sounds like it is used to specify how many hops you expect the trace route to get through in its test.

    So, with this script not being correct, I believe the results we get from it can tell us something. If this script fails when "-Hops 1" is specified, does that mean PowerShell was unable to contact the very first device in the trace route?

The topic ‘Test-NetConnection -Hops’ is closed to new replies.