So let's have a look at how we remove
completed background jobs. Okay, so now
that we actually have jobs, if I choose
Get‑Job you can see it gives me the list
of the jobs that are available. Now how do
we remove those? Well to remove a job we
can actually go and retrieve the job by a
specific name. So I can say ‑Name, and my
name is called, let me just use a capital
N, Notepad. So that retrieves me the job
and says it's failed. So how do I remove
that? Well if we use the same syntax as
before, I'm going to go ahead and populate
my variable, let me just clear that, we'll
then go to $job and you can see that my
job is now available, so job, etc. And I
have it in an object. At this point I can
then use standard syntax to just say $job
|, pipe obviously pushes it to the next
one, and then I can remove the job. Now if
I choose get‑job you can see that that job
that was Id of 31 no longer exists because
it's been removed. Now what about if we
wanted to get the instance Ids of the jobs
themselves? Well, let's go through and
choose Get‑Job. And, I tell you what,
let's populate the variable again. We'll
populate add $job variable. We'll say
Get‑Job. And I'm going to choose an Id,
and we'll choose 27. Okay, so I have the
job Id of 27. Now what I can do is, if we
just clear this so we can see it, $job.
I'm going to use the pipe command again.
And then I can use standard PowerShell
values, so I can say ‑Property * and then
Enter. Now notice this output that we get.
From a specific job we get a more detailed
view of what took place. So you can see we
get the current state, if it has any more
data, a current status message, we get
obviously the command that was executed,
the date, the time, etc, but importantly
here we get this which is the instance Id.
Now what that means is I can then go in
and say Remove‑Job ‑InstanceId. I can then
paste my instance Id and then that will
remove that specific job. So if we just
click on our job option, it was Job27. So
if I choose Get‑Job, you can see Job27 no
longer exists. So, it's fairly
straightforward. Now of course, the
question really comes down to how do we
get jobs back that weren't created using
the standard job, you know, Start‑Job, or
using the ampersand one. So let's say we
initiated an Invoke‑Command. So of course
we just did that, we invoked a command. So
let me just go back through my commands
here. We should be able to see an
Invoke‑Command. So I'm going to say
create. That's going to issue my same
command. So you'll see my Notepad one is a
remote job. So how do I remove that one?
Well, we can remove that one on the
machine that it's executing on by using
another Invoke‑Command. So think this
logic through. I used Office, which is
this machine, but if I was talking to a
different machine that job would be
executing over there. Okay, so if we
wanted to utilize like the session type Id
to remove something that we'd invoked on a
different machine then of course we would
create the session so New‑PSSession,
remember this is going to be a
non‑interactive session, and I can just
use the name of the computer locally and
that creates me a session. Now that could
have been a remote computer. It doesn't
really make a difference. If I type
session now you'll see I have a Runspace
session that's connected using PowerShell
remoting to this remote machine. So let me
just clear that. Now what I can do here is
when I issue the Invoke‑Command request I
now get to specify what they session is.
And then of course I get to do the same
thing as we did before. So, writing a
ScriptBlock. And I'm going to start a job,
as we did before. So, let's do
‑ScriptBlock again and then let's do a bit
of a break here and we'll choose
Get‑Process. I'm also going to give this a
name because obviously I want to be able
to call this job again. So I'll call it
LocalhostJob. And there we have it. Okay,
so that's kicked off a remote job
technically on the same machine, but using
the remote mechanism instead. So, let's go
back and say Get‑Job, and you'll see it
gives me a list of jobs that are available
here. So we've got background jobs, we've
got jobs that are run locally, you can see
they're run on the localhost, and then
we've got this kind of remote job, which
was a Notepad one that I ran. And then if
you notice, the one in the top section,
which is the PS job that says Id of 1
doesn't exist in this table. So where does
that job actually exist? Well, that's
where we can use the Invoke‑Command again.
So Invoke‑Command. We'll use the same
session, which is the non‑interactive
session. And then what I'm going to do is
do a ScriptBlock. And then from here I'm
just going to type Get‑Job and then Enter
that. And you'll notice it now goes into
the session which was created, which could
be a Windows machine, you know, a server,
a desktop, whatever it would be, and shows
me those specific jobs. They don't show up
as jobs on that specific machine because
they are classed as remote jobs. Now to
remove those, I can simply come in here
and just say Remove‑Job. I can then give
it a job name if I wanted to. And then the
name that we gave it was LocalhostJob.
Okay, so I'm going to invoke a request to
the current session and go and delete that
job. Okay, so let's re‑execute the one
from before, and now there's no jobs. And
if I run Get‑Job it just shows me the jobs
that are local to this machine. So when
you're removing the jobs it's important to
understand how to get a hold of them. So
if it's ran by the Start‑Job or by the
ampersand operator then they'll be local.
If you use a new PS session they'll be in the context of that specific machine.