So let's go ahead and let's create some
background jobs utilizing each of those
approaches. Okay, so we're back on the
PowerShell client. Now in order for us to
create a job, there's obviously a couple
different ways of doing this. So the first
one we did look at was Start‑Job. And this
one, obviously, we can pass a ScriptBlock.
We use our squirrely brackets is before.
So I'm going to stick to use in the
Get‑Process one. And we'll pass a name,
and we'll look for Notepad. Okay, so our
job is now created. Fairly
straightforward. Just the standard way of
creating a job. Now let's say we just
wanted to get that process, let's say by
name, and we'll use Notepad again, and I
wanted to create it as a job. Well now I
can just pass in the ampersand prompt
parameter at the end, and you'll see it
now creates a job automatically. So if I
now choose Get‑Job, you'll see those two
jobs exist. Both were created, one with
Start‑Job and then one with the operator
using the ampersand character. Now the
third option is I'm going to create a
variable called job, and I'm going to use
Invoke‑Command. And then from here, I'm
just going to say ScriptBlock, and we'll
use our squirrely brackets as before. I'm
going to say Get‑Process. Exactly the same
as we did before. Notepad. Then I'm going
to go and say JobName, and we'll call this
one Notepad so we can differentiate it.
And then I'm going to use this AsJob
parameter. So that's going to be
Invoke‑Command ScriptBlock, give it a
name, and then do AsJob. Now what does it
say when it comes up? It says parameter
set cannot be resolved using the specified
name parameters. One or more parameters
issued cannot be used together or there's
an insufficient number of parameters. So
what does that mean? Well, it tells me
that there's a parameter that's missing.
So in actual fact, if we go to
Invoke‑Command, what we can actually do
here is if we just drop into this one, you
can see that there's a ComputerName option
that's available. Now my ComputerName is
called OFFICE, so I'm going to press
Enter. And notice this time it did
something. It didn't error. It just did
something. So what does that look like?
Well, if I just type job and enter, you'll
see that it created a new type of job. Now
just wait, it says RemoteJob versus a
regular job because I passed in a computer
name. Now albeit the same name as the
computer that I'm on, but that created a
RemoteJob. Now if we go back to my syntax
and let's just do Get‑Name one that's
there, you can see that the first one that
I created is the background job, and the
second one is a remote job. So just be
aware depending on how you decide to
create the job or dictate the type of job
that gets create. Now when we actually
create jobs, we can obviously start to be
very creative. So if I do Start‑Job, I can
do ScriptBlock, and then maybe I want to
start a job in here as well if I wanted to
because that's also a command. Or I could
say, let's do Get‑Process and Notepad. And
then, of course, I can then start to
expand that script process. Maybe I do
want to go ahead and say Start‑Job
ScriptBlock and then start to nest
specific commands. Now these are not the
easiest to do because you're obviously
starting to nest commands inside of each
other. But you do have the flexibility of
saying well, I have this process, then I
have this process, then I have another
process that I would like to kind of
inject into that same. Now if I try to
execute this, I want you to see what
happens. Okay, so it created a background
job. But what job did it create? Well,
there was a job and then I told it to
create a job. And then I told it to get a
process. So this is where we can actually
use the Get‑Job option, and I can say
include children. And what you'll see is
it goes and gets me all the list of jobs,
and you can see that it lists me out all
of the jobs that got created. So you can
see I've got my Job35 at the bottom, which
is my background job. And then I've got my
child job, which is Job36, which is the
one that was created. So we can see that
we can start to name and at least condense
all of those jobs into one kind of
command. So let me just clear that. So it
is a little bit more complicated, I will
be honest with you, to try and nest them
all together. Where possible, you probably
just want to utilize a standard, either
Start‑Job or maybe an Invoke command and then nest the jobs as needed there.