Link to home
Start Free TrialLog in
Avatar of Jeremy Weisinger
Jeremy Weisinger

asked on

Storing the results of the Test-Connection

This one has baffled me and I haven't been able to figure this out within powershell. (I'm able to dump the results to a file)

I've tried these variations on the command:
$iplist = 1..5 | %{"10.0.0."+$_}

$a = $iplist | %{Test-Connection $_}

$b = Test-Connection $iplist

$job = Test-connection -ComputerName $iplist -asjob
$c = Receive-Job $job

Open in new window


The first two run the command and then when I try and view the variable it seems to test the connection again. The one where I run as a job completes immediately and when I try and view the retrieved results it seems to run the test again.

Does anyone know why this happens (I can't think of anything else I've used in Powershell that behaves like this) and how I can store the results of the test?
This occurs on Windows 2008 R2 through current that I've seen.
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeremy Weisinger
Jeremy Weisinger

ASKER

Thanks Jose.
 
I see that using Start-Job works but can you explain what is different between Start-Job and using -asjob and why it works?
Well there is no difference,
Basically what it does the Start-job is to create a background job of a script to run asynchronously or synchronously), this means that you can in the "$scriptblock" variable start other jobs and I think is more versatile and the code is clearer (besides the few additional lines and the way to proceed with the parameters).

The as-job, just create a single background job from each iteration and add it to the array of "$AllAsJobs", then wait for all the jobs and then receive the jobs.

The initial block in the as-job answer is just to clear any remaining job from previous runs and start fresh.

Also, the point in here is that the jobs don't contain any data until they are done, that's why you use the "wait-job" cmdlet.
Well there is no difference
Well there has to be a difference since one seems to get the output and the other seems to store the command, right?

I do understand and have used *-Job cmdlets so that's not really what I was wanting to know. I do understand the entirety of your script. I just don't understand why using Start-Job causes different behaviour.

The workaround is great but I really want to understand why (and Microsoft needs to update their help file on it because their examples don't work).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thought that too (That the -asjob stores the command), even I tried to run or "force the run" of the background job (by thinking the way you do) but those attempts failed, at the end my conclusion was that it creates the task at the background directly.

So at the end it works and behaves the same way, the only difference is the Wait-job command. That's what I can say about my findings of answering your question.

I haven't worried about the version, so that could be the reason, I used version 5.1
I've tested on 5.1 on Win 10 (insider slow ring) , 4.0 on Win 8.1 and Win 2012 R2, 3.0 on Win 2012, and 2.0 on Win 2008 R2. Version 3.0 and 2.0 didn't have the issues.

So that's weird. I had tested it out on a 2008 R2 server just before posting this and now it doesn't seem to be happening.

OK, so at this point I think this is a MS bug in, I suppose, certain versions of Powershell.

Thanks Jose and Footech!