Search code examples
powershellpingjobs

Powershell Test-Connection: different results when run AsJob vs straight-up


Short version: Why does Test-Connection provide only one result when running -AsJob?


Detailed version:

When the Test-Connection (ping!) command is run in Powershell (running as Administrator) it uses the default value of 4 for the Count parameter. This gives me the expected results (some irrelevant data removed for clarity):

PS C:\Windows\system32> Test-Connection www.stackoverflow.com

Destination     IPV4Address      IPV6Address   Bytes    Time(ms) 
-----------     -----------      -----------   -----    -------- 
www.stackove... 64.34.119.12     {}            32       117      
www.stackove... 64.34.119.12     {}            32       113      
www.stackove... 64.34.119.12     {}            32       111      
www.stackove... 64.34.119.12     {}            32       113     

However, when I run it with the -AsJob flag, it seems (when running Receive-Job, after waiting a few secs) the Count was only 1, but I expected it to be 4 and get similar results as above. Instead I get this:

PS C:\Windows\system32> Test-Connection www.stackoverflow.com -AsJob

Id         Name            State      HasMoreData     Location     Command        
--         ----            -----      -----------     --------     -------        
1          Job1            Running    False           .            Test-Connection

PS C:\Windows\system32> Receive-Job 1

Destination     IPV4Address      IPV6Address   Bytes    Time(ms) 
-----------     -----------      -----------   -----    -------- 
www.stackove... 64.34.119.12     {}            32       113  

The AsJob variant always returns one row, even if you provide a specific value for Count.

My Google-fu and Get-Help-fu don't seem to be sufficient. Can someone please explain what's going on here, preferably with some pointers how and where to find this info myself (e.g. which help files or sources explain this behavior)?


Solution

  • Could be a bug. In v3 you get back 4 pings.