Okay, so we've started, we've removed, and
we've waited. Now how do we know what's
going on, and what information can we
retrieve from a background job? Well there
are two options here, the first one is to
call get‑job. This one gets the current
PowerShell background jobs that are
running in that current session. So in my
PowerShell window, if I've initiated 10
jobs, if I choose get‑job, it'll return
those 10 jobs. The second one is
receive‑job. This will get the results or
the ending part of the PowerShell
background job for that current session.
So if my job was to retrieve a list of
processes, if I choose get‑job, it'll give
me the status. If I choose receive‑job, it
will output the results for that job. So
let's talk about using get‑job. If I want
to retrieve the job details, let's say I
initiate a start‑job, and it's going to
get the processes, and I give it a name, I
can then just say get‑job, name, and job
because that's the name that I associated
to it, and then that populates my job
variable that I can then output with
values, and it will give me the date, the
time, the ID, the status, did it fail, did
it complete, etc. I could also get child
job details. Now you might not actually
execute child jobs, but what happens is in
the hierarchy of jobs, when you create a
background job, it will create the initial
one, and then it can spawn off child jobs
to execute the various components under
the covers. So for example, if we take the
one at the top here that says Start‑Job
and then Get‑Process, you'll actually end
up with two jobs, a parent job and a
child. The child job will be Get‑Process.
So you can include the children, or the
child processes in this one, and get a
status of them. I could also get
non‑started jobs, or failed, etc., by
passing in the ‑state parameter and
specifying a value. Now if I wanted to
retrieve the results from a specific job,
then of course we'll use receive‑job. Same
process again. I'm going to start the job,
Get‑Process to list me all the processes,
and then I'm going to receive the job, and
then that will output the value of that,
whatever the results are supposed to be.
We could also do this when we're looking
at specific computers. So maybe I've got a
domain controller, a SQL server, and a web
server, and I want to be able to get the
computer name, for example. So I'm using
environment COMPUTERNAME. In this
instance, I'm going to create a new
PowerShell session, target these computer
names, used the Invoke‑Command for those
sessions, and then pass in the
information. And then I can simply call
get‑job, and this will then return the
values for that one. Then I can pass in
receive‑job to get the results when they've, once they've completed.