So let's go into our PowerShell, and we'll
look at reviewing PowerShell background
job commands and how to create some of
those jobs. Okay, so we're on our
PowerShell 7 console. Actually, this is
the Windows 10 terminal console. So first
off, what I want to do is actually just
look at some of the variables that are
available. If I click $PSVersionTable, you
can see it will give me the current
version, so you can see I'm using 7.0.3,
the Core edition, and it tells me I'm
using Windows 10, etc. If I just want to
look at a specific property, I can say
$PSVersion, and it will give me the Major,
Minor, Patch, etc. Now, of course, what we
want to do here is look at how we create
those jobs. So our first syntax here is
Start‑Job. But, actually, what other job
commands are available? We can say
Get‑Command. And then what I'm going to do
is just do *job* and there. And you'll see
that when I look in the list, if I scroll
back up, you can see PrintJob, StorageJob,
PrintJob, Debug‑Job, Get‑Job, JobTrigger,
Remove. So there's a whole host of
different types of jobs that are
available. So tell you what, let's just
clear that screen again. And this time
I'll get rid of the star and just do job.
Now, of course, that doesn't work because
it can't find something that's
specifically just job. So if I do star,
then, of course, you'll see some of those
job ones disappear, and some of them
don't. But either way, you can kind of see
the version of the PowerShell that it's
part of. But the most important ones will
be Wait‑Job, Stop‑Jobs, Start‑Job,
Remove‑Job, Receive, Get‑Job, etc. Now
these commands themselves, if I click
Start‑Job here and just do Enter, the
default property will be the ScriptBlock
itself, which is going to ask me for
something to happen. Now if I look at what
would be a standard one, it would be
Get‑Process. And, of course, notice what
happens. It says, Well, I can't take that
value because I don't quite know what
that's doing. If I type Get‑Process, for
example, here, that's going to execute
anyway. So why does it not work in the
Start‑Job? And that's because if we look
at the Start‑Job syntax here and just do
‑ScriptBlock, a ScriptBlock needs to be
encompassed inside the squirrelly brackets
here. So at this point, I can say
Get‑Process and execute, and the job then
works. So just be aware that you can't
just paste whatever you want to into that
ScriptBlock. Now let's say I want to look
at the other property, so I've got the
‑Name, so I can put a name here, so
TestJob. Then if we go a bit further,
we've got Credentials. I've got
Authentication. I've got
InitializationScript, the
WorkingDirectory, 32‑bit runners, etc.,
and I can keep going. If I want to choose
Verbose, for example, I can do Verbose.
Now there's nothing that comes back
because, literally, when you kick off a
job, that's where it ends up. So it's
fairly straightforward. Now if I choose
Get‑Job, which is another command, this'll
list me the jobs that are there, and you
can see that my TestJob there is actually
in the middle of executing. So let me just
clear that one. If I choose Get‑Job again,
you'll see I can change this and say ‑Id,
and I'll choose 23. So this will now
return back and say Job 23, and it's been
completed. I could also then choose other
properties such as ‑IncludeChildJob, and
you'll see this is an introduction here,
this piece. You'll see that the parent is
a job, and then the actual job or the
command that executes is a child job. We
also have the ability to remove a job
also. That is also done by the ID or the
instance of the ID. Now, if we go back to
Get‑Job, you can see I've got my numbers
here. I can say Get‑Job, and then I can
say, I don't want Remove‑Aip, Remove‑Job,
and Enter. If I now choose Get‑Job,
there'll be no jobs available. So the
basic commands are really simple. You
obviously need to understand which pieces
to pass into it, such as the properties,
the various variables. For example, if we
go back to our Start‑Job command and then
do a ‑ScriptBlock, we have our squirrelly
brackets here, and then I'm going to
choose Get‑Process. In here I have
whatever the properties are for the
Get‑Process. So just be aware that as we
join things together, we can get quite
nested in the types of commands that we
can pass. So if I just choose, Get‑Process
‑Name pwsh for PowerShell, I can start the
job, and then when it's finished, if I
just go back here, and we can choose
Get‑Job, Get‑Job, it'll say it's completed
now. And so I have that job executed. Now
what does that mean though? Well, the job
executed, and I have no information that's
come back. So I can say Receive, I got the
e and the i in the wrong way, Receive‑Job,
and I can pick a job ID and say 25, and,
sure enough, it comes back and says, Yep,
there are your processes. So notice that
the tie there where we have the job that
was created, we can retrieve the job
status, we can remove the jobs, and then
we have Receive‑Job, which returns the values that are needed from that job.