Okay, so let's go and have a look at how
we remove remote background jobs. Okay, so
what we looked at last time was the
creation of the jobs, but what about if we
wanted to remove a specific job? Well,
first off, we know that Get‑Job is going
to retrieve any jobs, and we also know
that if I say Remove‑Job, and pass in the
Id, that that will clear those jobs down.
And that's a local job. But what about a
remote job? So let me go back into my
session, and you can see I still have my
existing session created. So how do I get
those jobs from that remote machine? So
what we can do here is I could say. let me
just clear this, I could say $job, which
is going to be the job that I want to get
back. I could say Invoke‑Command, and I'm
going to pass it into the session, so this
session to the remote machine, and then
I'm going to say ScriptBlock. And then of
course we pass in some various options,
and I'm going to say Get‑Job. So let's
just execute that. Now what does that look
like? Well, there we go. It brings back
the job from the remote machine. So I
basically invoke a command to use Get‑Job.
So how do I remove that job because now
I've got that one. What I can actually do
is I can say $job, and if I say Get‑Job,
and notice what happens, it says the
command cannot find a job with the job ID
1. That's because remember, where does job
1 exist, not locally, and the standard
Get‑Job command is tied to this local
machine here. So how do we get rid of that
specific one? Well, let's try doing job,
and we'll do that. And then we'll say
Remove‑Job, and we'll do that. Oh, wait,
same issue. It comes back as a failure. So
how do we clear the jobs down? Well, let's
think about this. It's going to be where
we can actually say Invoke‑Command, push
it into the existing session on the remote
one, say ScriptBlock, and then I'm going
to say Remove‑Job ‑Id 1 because I already
know that. Okay, so I initiated a
connection, and I told it to remove. Now I
have a connection here. So how do we get
the jobs back for that? Well think of what
we just did, we just did remove. We know
that the Get‑Job command will return jobs,
and sure enough, it's been deleted. So we
can remove jobs utilizing the same
mechanism that we did for creating of jobs
and getting jobs from the remote machines.
So when we execute the job on, you know,
multiple computers, to retrieve or remove
that, we need to go back into the existing
session, so that we can then execute the commands using an invoke command.