Leonel Goncalves
Leonel Goncalves

Reputation: 1

How to get data from job before it ends?

I'm doing a project where I need to start jobs to do parallel processes. I'm using Start-Job to launch them, and I can get data on the end of the job with Receive-Job. The problem here is: the process is to copy files from directories and this process can takes long, so I need to check if processing is going OK or stacked somewhere, like a percentage (%) of files copied.

How can I check a variable value before the job ends?

Upvotes: 0

Views: 67

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

You can call Receive-Job at any time. The job doesn't have to be finished for that. The received output is removed from the job, though, so you need to store it in a variable if you need it later on.

Demonstration:

PS C:\> $job = Start-Job -ScriptBlock {1..10 | %{$_; Start-Sleep -Seconds 1}}
PS C:\> Start-Sleep -Seconds 3
PS C:\> Receive-Job -Job $job
1
2
3
PS C:\> Start-Sleep -Seconds 3
PS C:\> Receive-Job -Job $job
4
5
6
PS C:\> $job.State
Running

Upvotes: 1

Related Questions