1
00:00:00,640 --> 00:00:02,810
So let's have a look at how we remove

2
00:00:02,810 --> 00:00:06,040
completed background jobs. Okay, so now

3
00:00:06,040 --> 00:00:09,020
that we actually have jobs, if I choose

4
00:00:09,020 --> 00:00:11,520
Get‑Job you can see it gives me the list

5
00:00:11,520 --> 00:00:14,560
of the jobs that are available. Now how do

6
00:00:14,560 --> 00:00:17,600
we remove those? Well to remove a job we

7
00:00:17,600 --> 00:00:20,050
can actually go and retrieve the job by a

8
00:00:20,050 --> 00:00:24,090
specific name. So I can say ‑Name, and my

9
00:00:24,090 --> 00:00:27,490
name is called, let me just use a capital

10
00:00:27,490 --> 00:00:31,080
N, Notepad. So that retrieves me the job

11
00:00:31,080 --> 00:00:33,330
and says it's failed. So how do I remove

12
00:00:33,330 --> 00:00:35,450
that? Well if we use the same syntax as

13
00:00:35,450 --> 00:00:38,040
before, I'm going to go ahead and populate

14
00:00:38,040 --> 00:00:40,490
my variable, let me just clear that, we'll

15
00:00:40,490 --> 00:00:42,980
then go to $job and you can see that my

16
00:00:42,980 --> 00:00:45,920
job is now available, so job, etc. And I

17
00:00:45,920 --> 00:00:49,530
have it in an object. At this point I can

18
00:00:49,530 --> 00:00:53,510
then use standard syntax to just say $job

19
00:00:53,510 --> 00:00:55,730
|, pipe obviously pushes it to the next

20
00:00:55,730 --> 00:00:58,080
one, and then I can remove the job. Now if

21
00:00:58,080 --> 00:01:00,800
I choose get‑job you can see that that job

22
00:01:00,800 --> 00:01:03,960
that was Id of 31 no longer exists because

23
00:01:03,960 --> 00:01:07,020
it's been removed. Now what about if we

24
00:01:07,020 --> 00:01:10,330
wanted to get the instance Ids of the jobs

25
00:01:10,330 --> 00:01:13,020
themselves? Well, let's go through and

26
00:01:13,020 --> 00:01:16,910
choose Get‑Job. And, I tell you what,

27
00:01:16,910 --> 00:01:18,380
let's populate the variable again. We'll

28
00:01:18,380 --> 00:01:20,840
populate add $job variable. We'll say

29
00:01:20,840 --> 00:01:23,750
Get‑Job. And I'm going to choose an Id,

30
00:01:23,750 --> 00:01:29,470
and we'll choose 27. Okay, so I have the

31
00:01:29,470 --> 00:01:33,690
job Id of 27. Now what I can do is, if we

32
00:01:33,690 --> 00:01:37,280
just clear this so we can see it, $job.

33
00:01:37,280 --> 00:01:39,320
I'm going to use the pipe command again.

34
00:01:39,320 --> 00:01:42,990
And then I can use standard PowerShell

35
00:01:42,990 --> 00:01:46,210
values, so I can say ‑Property * and then

36
00:01:46,210 --> 00:01:49,250
Enter. Now notice this output that we get.

37
00:01:49,250 --> 00:01:53,230
From a specific job we get a more detailed

38
00:01:53,230 --> 00:01:55,930
view of what took place. So you can see we

39
00:01:55,930 --> 00:01:58,150
get the current state, if it has any more

40
00:01:58,150 --> 00:02:01,080
data, a current status message, we get

41
00:02:01,080 --> 00:02:04,140
obviously the command that was executed,

42
00:02:04,140 --> 00:02:06,890
the date, the time, etc, but importantly

43
00:02:06,890 --> 00:02:10,740
here we get this which is the instance Id.

44
00:02:10,740 --> 00:02:13,810
Now what that means is I can then go in

45
00:02:13,810 --> 00:02:19,950
and say Remove‑Job ‑InstanceId. I can then

46
00:02:19,950 --> 00:02:22,260
paste my instance Id and then that will

47
00:02:22,260 --> 00:02:24,970
remove that specific job. So if we just

48
00:02:24,970 --> 00:02:28,340
click on our job option, it was Job27. So

49
00:02:28,340 --> 00:02:31,910
if I choose Get‑Job, you can see Job27 no

50
00:02:31,910 --> 00:02:34,490
longer exists. So, it's fairly

51
00:02:34,490 --> 00:02:36,590
straightforward. Now of course, the

52
00:02:36,590 --> 00:02:39,180
question really comes down to how do we

53
00:02:39,180 --> 00:02:43,440
get jobs back that weren't created using

54
00:02:43,440 --> 00:02:46,940
the standard job, you know, Start‑Job, or

55
00:02:46,940 --> 00:02:49,530
using the ampersand one. So let's say we

56
00:02:49,530 --> 00:02:52,680
initiated an Invoke‑Command. So of course

57
00:02:52,680 --> 00:02:54,570
we just did that, we invoked a command. So

58
00:02:54,570 --> 00:02:55,970
let me just go back through my commands

59
00:02:55,970 --> 00:02:57,720
here. We should be able to see an

60
00:02:57,720 --> 00:02:59,230
Invoke‑Command. So I'm going to say

61
00:02:59,230 --> 00:03:02,290
create. That's going to issue my same

62
00:03:02,290 --> 00:03:04,810
command. So you'll see my Notepad one is a

63
00:03:04,810 --> 00:03:08,940
remote job. So how do I remove that one?

64
00:03:08,940 --> 00:03:12,660
Well, we can remove that one on the

65
00:03:12,660 --> 00:03:15,420
machine that it's executing on by using

66
00:03:15,420 --> 00:03:18,010
another Invoke‑Command. So think this

67
00:03:18,010 --> 00:03:20,630
logic through. I used Office, which is

68
00:03:20,630 --> 00:03:22,610
this machine, but if I was talking to a

69
00:03:22,610 --> 00:03:24,620
different machine that job would be

70
00:03:24,620 --> 00:03:28,800
executing over there. Okay, so if we

71
00:03:28,800 --> 00:03:31,970
wanted to utilize like the session type Id

72
00:03:31,970 --> 00:03:34,550
to remove something that we'd invoked on a

73
00:03:34,550 --> 00:03:36,850
different machine then of course we would

74
00:03:36,850 --> 00:03:40,460
create the session so New‑PSSession,

75
00:03:40,460 --> 00:03:41,580
remember this is going to be a

76
00:03:41,580 --> 00:03:45,130
non‑interactive session, and I can just

77
00:03:45,130 --> 00:03:48,520
use the name of the computer locally and

78
00:03:48,520 --> 00:03:50,260
that creates me a session. Now that could

79
00:03:50,260 --> 00:03:52,180
have been a remote computer. It doesn't

80
00:03:52,180 --> 00:03:54,610
really make a difference. If I type

81
00:03:54,610 --> 00:03:56,730
session now you'll see I have a Runspace

82
00:03:56,730 --> 00:03:59,610
session that's connected using PowerShell

83
00:03:59,610 --> 00:04:02,860
remoting to this remote machine. So let me

84
00:04:02,860 --> 00:04:05,790
just clear that. Now what I can do here is

85
00:04:05,790 --> 00:04:09,510
when I issue the Invoke‑Command request I

86
00:04:09,510 --> 00:04:13,440
now get to specify what they session is.

87
00:04:13,440 --> 00:04:14,970
And then of course I get to do the same

88
00:04:14,970 --> 00:04:17,930
thing as we did before. So, writing a

89
00:04:17,930 --> 00:04:21,540
ScriptBlock. And I'm going to start a job,

90
00:04:21,540 --> 00:04:24,390
as we did before. So, let's do

91
00:04:24,390 --> 00:04:28,210
‑ScriptBlock again and then let's do a bit

92
00:04:28,210 --> 00:04:30,660
of a break here and we'll choose

93
00:04:30,660 --> 00:04:33,830
Get‑Process. I'm also going to give this a

94
00:04:33,830 --> 00:04:35,470
name because obviously I want to be able

95
00:04:35,470 --> 00:04:38,970
to call this job again. So I'll call it

96
00:04:38,970 --> 00:04:43,270
LocalhostJob. And there we have it. Okay,

97
00:04:43,270 --> 00:04:46,840
so that's kicked off a remote job

98
00:04:46,840 --> 00:04:48,760
technically on the same machine, but using

99
00:04:48,760 --> 00:04:52,610
the remote mechanism instead. So, let's go

100
00:04:52,610 --> 00:04:55,400
back and say Get‑Job, and you'll see it

101
00:04:55,400 --> 00:04:57,750
gives me a list of jobs that are available

102
00:04:57,750 --> 00:04:59,480
here. So we've got background jobs, we've

103
00:04:59,480 --> 00:05:02,070
got jobs that are run locally, you can see

104
00:05:02,070 --> 00:05:03,960
they're run on the localhost, and then

105
00:05:03,960 --> 00:05:05,590
we've got this kind of remote job, which

106
00:05:05,590 --> 00:05:07,930
was a Notepad one that I ran. And then if

107
00:05:07,930 --> 00:05:09,720
you notice, the one in the top section,

108
00:05:09,720 --> 00:05:12,830
which is the PS job that says Id of 1

109
00:05:12,830 --> 00:05:16,910
doesn't exist in this table. So where does

110
00:05:16,910 --> 00:05:20,350
that job actually exist? Well, that's

111
00:05:20,350 --> 00:05:24,740
where we can use the Invoke‑Command again.

112
00:05:24,740 --> 00:05:27,260
So Invoke‑Command. We'll use the same

113
00:05:27,260 --> 00:05:29,980
session, which is the non‑interactive

114
00:05:29,980 --> 00:05:32,040
session. And then what I'm going to do is

115
00:05:32,040 --> 00:05:34,760
do a ScriptBlock. And then from here I'm

116
00:05:34,760 --> 00:05:37,470
just going to type Get‑Job and then Enter

117
00:05:37,470 --> 00:05:39,630
that. And you'll notice it now goes into

118
00:05:39,630 --> 00:05:41,830
the session which was created, which could

119
00:05:41,830 --> 00:05:44,390
be a Windows machine, you know, a server,

120
00:05:44,390 --> 00:05:46,720
a desktop, whatever it would be, and shows

121
00:05:46,720 --> 00:05:50,230
me those specific jobs. They don't show up

122
00:05:50,230 --> 00:05:53,540
as jobs on that specific machine because

123
00:05:53,540 --> 00:05:56,300
they are classed as remote jobs. Now to

124
00:05:56,300 --> 00:05:58,800
remove those, I can simply come in here

125
00:05:58,800 --> 00:06:07,150
and just say Remove‑Job. I can then give

126
00:06:07,150 --> 00:06:11,090
it a job name if I wanted to. And then the

127
00:06:11,090 --> 00:06:18,540
name that we gave it was LocalhostJob.

128
00:06:18,540 --> 00:06:20,900
Okay, so I'm going to invoke a request to

129
00:06:20,900 --> 00:06:22,940
the current session and go and delete that

130
00:06:22,940 --> 00:06:25,780
job. Okay, so let's re‑execute the one

131
00:06:25,780 --> 00:06:28,030
from before, and now there's no jobs. And

132
00:06:28,030 --> 00:06:31,100
if I run Get‑Job it just shows me the jobs

133
00:06:31,100 --> 00:06:33,740
that are local to this machine. So when

134
00:06:33,740 --> 00:06:35,560
you're removing the jobs it's important to

135
00:06:35,560 --> 00:06:37,730
understand how to get a hold of them. So

136
00:06:37,730 --> 00:06:40,610
if it's ran by the Start‑Job or by the

137
00:06:40,610 --> 00:06:43,410
ampersand operator then they'll be local.

138
00:06:43,410 --> 00:06:49,000
If you use a new PS session they'll be in the context of that specific machine.

