Jacob
Jacob

Reputation: 1192

Test-Connection with -asjob and -delay

When running Test-Connection as a job it seems to completely disregard the -delay parameter.

For example when using "-delay 5" and "-count 10" it would be expected this should take roughly 50 seconds to complete.

When testing with Measure-Command this confirms that when run as a job this is not the case, as you can see the job took less that a second to complete, but when not run as a job took roughly 50 seconds as expected.

Measure-Command {Test-Connection google.co.uk -Count 10 -Delay 5 -AsJob | where {$_.Name = $Target}}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 26
Ticks             : 263563
TotalDays         : 3.05049768518519E-07
TotalHours        : 7.32119444444444E-06
TotalMinutes      : 0.000439271666666667
TotalSeconds      : 0.0263563
TotalMilliseconds : 26.3563

Measure-Command {Test-Connection google.co.uk -Count 10 -Delay 5}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 45
Milliseconds      : 445
Ticks             : 454456692
TotalDays         : 0.000525991541666667
TotalHours        : 0.012623797
TotalMinutes      : 0.75742782
TotalSeconds      : 45.4456692
TotalMilliseconds : 45445.6692

I have tested using "-count 1000" with "-asjob" and even this has minimal impact on the time it takes to execute.

I have had a look around but could find no explanation for this and was curious if anyone can explain why this behaviour occurs?

Additionally is there another method to invoke test-connection with the -delay parameter to run parallel with the rest of my script retrieve the results of test-connection at the end?

Update #1

I can see from the responses my use of the Measure-Command is wrong, but regardless if you take the following snippet:

$start = Get-date
$target = "google.co.uk"
$count = 10
Test-Connection $target -Count $count -Delay 5 -AsJob | where {$_.Name = $target} 
$result = Wait-Job $Target | Receive-Job
Remove-Job $target
$result
$end = Get-date
""
Write-host "Completed in: $($end - $start)"

This outputs

    Source        Destination     IPV4Address      IPV6Address                     
------        -----------     -----------      -----------                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     
SURFACE       google.co.uk    216.58.204.3                                     

Completed in: 00:00:00.1170733

Whereas

$start = Get-date
$target = "google.co.uk"
$count = 10
Test-Connection $target -Count $count -Delay 5
$end = Get-date
""
Write-host "Completed in: $($end - $start)"

Returns

SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   
SURFACE       google.co.uk    216.58.206.131                                   

Completed in: 00:00:45.4695467

I'm not sure if there is some fundamental flaw in my methodology.

Maybe my original questions wasn't as clear as it could have been, The reason I was using Measure-Command was to try and quantify my observation.

I'm running the test-connection as a job to perform 120 pings 1 second apart, so I would expect that job to not be available for approx. 120 seconds. But I can return the job and output the results in just a few seconds. Which isn't what I am expecting.

Upvotes: 0

Views: 1162

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

That's because you're not actually waiting for the jobs to finish. It only takes a few milliseconds to spin up the individual jobs and return the PSJob objects.

Compare with:

Measure-Command {
    Test-Connection google.co.uk -Count 10 -Delay 5 -AsJob |Receive-Job -Wait
}
# and
Measure-Command {
    Test-Connection google.co.uk -Count 10 -Delay 5
}

Upvotes: 1

Related Questions