1
00:00:00,440 --> 00:00:01,890
So let's go back and look at how we

2
00:00:01,890 --> 00:00:05,990
retrieve background job information about

3
00:00:05,990 --> 00:00:08,130
local jobs and also other jobs that we

4
00:00:08,130 --> 00:00:11,120
create. Okay, so once we have jobs

5
00:00:11,120 --> 00:00:13,450
running, we obviously want to get details

6
00:00:13,450 --> 00:00:15,850
about what took place with the job. So

7
00:00:15,850 --> 00:00:17,970
what I'm going to do here is I'm just

8
00:00:17,970 --> 00:00:20,820
going to start a new job. So we'll just do

9
00:00:20,820 --> 00:00:22,760
the standard ‑ScriptBlock that we had

10
00:00:22,760 --> 00:00:27,480
before. We'll choose Get‑Process, and

11
00:00:27,480 --> 00:00:32,260
we'll close that off, and then what we'll

12
00:00:32,260 --> 00:00:34,840
do is we'll give it a name, and I'll call

13
00:00:34,840 --> 00:00:39,580
it Job1. So that kicks off the job. Now

14
00:00:39,580 --> 00:00:42,860
what I can do here is I can say $job. I

15
00:00:42,860 --> 00:00:45,740
can then use what's called Get‑Job and

16
00:00:45,740 --> 00:00:48,340
specify that by ‑Name. And then I can say

17
00:00:48,340 --> 00:00:52,460
job1, and I should then get the details of

18
00:00:52,460 --> 00:00:54,410
that job, which is what gets returned

19
00:00:54,410 --> 00:00:56,180
every time we execute a job. So you can

20
00:00:56,180 --> 00:00:59,550
see we get the ID, the name, the job type,

21
00:00:59,550 --> 00:01:02,640
the state, and whether it has more data.

22
00:01:02,640 --> 00:01:05,030
We can also then go one step further

23
00:01:05,030 --> 00:01:06,990
because, remember, whenever we execute a

24
00:01:06,990 --> 00:01:10,580
job, it has more than one job that takes

25
00:01:10,580 --> 00:01:12,290
place. So you can see we've got this whole

26
00:01:12,290 --> 00:01:14,460
host of things that have come down now, so

27
00:01:14,460 --> 00:01:18,060
I can go back here and say my job that I'm

28
00:01:18,060 --> 00:01:21,560
getting is this one. So I can say Get‑Job

29
00:01:21,560 --> 00:01:24,300
‑Name, and then I can choose the

30
00:01:24,300 --> 00:01:26,740
‑IncludeChildJob, and this narrows it down

31
00:01:26,740 --> 00:01:28,700
to that one specific job that we

32
00:01:28,700 --> 00:01:30,740
initiated. You'll notice that the

33
00:01:30,740 --> 00:01:33,470
background job is the initiation piece,

34
00:01:33,470 --> 00:01:36,900
and then any commands that were executed

35
00:01:36,900 --> 00:01:40,980
underneath that become a child job. What

36
00:01:40,980 --> 00:01:44,820
we can do at any point, so if I do this, I

37
00:01:44,820 --> 00:01:47,960
get the ‑State option, and then I can say

38
00:01:47,960 --> 00:01:51,920
Completed or I could say NotStarted, so if

39
00:01:51,920 --> 00:01:55,420
I type Not, it'll say NotStarted, and then

40
00:01:55,420 --> 00:01:57,220
it will return. Of course, I don't have

41
00:01:57,220 --> 00:01:59,390
anything here because there are no values

42
00:01:59,390 --> 00:02:01,930
for me to return back. But the Get‑Job

43
00:02:01,930 --> 00:02:05,410
option will return basic details about

44
00:02:05,410 --> 00:02:08,370
what took place with the job. Now what it

45
00:02:08,370 --> 00:02:12,020
doesn't do is return the value results

46
00:02:12,020 --> 00:02:15,920
from the job itself. So, for example, if I

47
00:02:15,920 --> 00:02:18,310
had done, for example, like we did the

48
00:02:18,310 --> 00:02:20,700
Get‑Process, so let me just choose

49
00:02:20,700 --> 00:02:24,460
Get‑Job, and we can see my list here. And

50
00:02:24,460 --> 00:02:26,710
if we look at number 52 which was our

51
00:02:26,710 --> 00:02:31,830
Job1, so let me just go job and ‑Id 52, we

52
00:02:31,830 --> 00:02:34,250
get the basic details. But what I don't

53
00:02:34,250 --> 00:02:37,470
see is what the end result was. So this is

54
00:02:37,470 --> 00:02:42,440
where we can utilize a different command

55
00:02:42,440 --> 00:02:47,160
called Receive‑Job. And I can then pass an

56
00:02:47,160 --> 00:02:49,470
‑Id. And then, of course, at this point,

57
00:02:49,470 --> 00:02:52,150
let's say it was 48, I could say retrieve

58
00:02:52,150 --> 00:02:57,600
job 48. That does not retrieve the job

59
00:02:57,600 --> 00:02:59,180
itself, you can see as I scrolled,

60
00:02:59,180 --> 00:03:01,790
because, remember, we issued a Get‑Process

61
00:03:01,790 --> 00:03:04,210
command, so the process will come back and

62
00:03:04,210 --> 00:03:06,300
give me all the processes of everything

63
00:03:06,300 --> 00:03:08,900
that's running on my machine, and that's

64
00:03:08,900 --> 00:03:11,860
what it returns. So the Receive‑Job

65
00:03:11,860 --> 00:03:16,250
command returns the values or the results

66
00:03:16,250 --> 00:03:18,850
from that. So let's do a simplistic one

67
00:03:18,850 --> 00:03:21,700
here. So I'm going to say $job =

68
00:03:21,700 --> 00:03:25,890
Start‑Job. I'm going to do a ‑ScriptBlock,

69
00:03:25,890 --> 00:03:27,640
and in here, I'm just going to use

70
00:03:27,640 --> 00:03:30,140
standard environment variables,

71
00:03:30,140 --> 00:03:33,990
COMPUTERNAME, and we'll do that. So my

72
00:03:33,990 --> 00:03:36,530
job's created. Now this one's not going to

73
00:03:36,530 --> 00:03:39,270
take long to actually execute, so I'm

74
00:03:39,270 --> 00:03:41,660
going to say Receive‑Job, and I'll do

75
00:03:41,660 --> 00:03:44,430
$job, and sure enough, it returns my value

76
00:03:44,430 --> 00:03:47,330
straightaway. So we can use this receive

77
00:03:47,330 --> 00:03:50,300
to get the results back. Now, if you think

78
00:03:50,300 --> 00:03:52,780
that through, if we're trying to retrieve

79
00:03:52,780 --> 00:03:56,030
information from other devices, so maybe I

80
00:03:56,030 --> 00:03:59,790
have a PS session, then we can do the same

81
00:03:59,790 --> 00:04:02,400
thing also, so we can actually go and get

82
00:04:02,400 --> 00:04:05,230
the job that was initiated, and we can

83
00:04:05,230 --> 00:04:07,920
receive the job. So let's think that

84
00:04:07,920 --> 00:04:10,030
process through. So let me just

85
00:04:10,030 --> 00:04:11,760
double‑check that we have my session.

86
00:04:11,760 --> 00:04:14,440
Okay, we still do. So what I'll do here is

87
00:04:14,440 --> 00:04:22,940
I'll say $job. I'm going to Invoke‑Command

88
00:04:22,940 --> 00:04:25,770
using my existing $session, which is the

89
00:04:25,770 --> 00:04:30,540
remote one, my ‑ScriptBlock. I'm actually

90
00:04:30,540 --> 00:04:35,470
going to start a job with a ‑ScriptBlock

91
00:04:35,470 --> 00:04:38,540
in there, and I'm going to use that same

92
00:04:38,540 --> 00:04:43,140
one that we just did, COMPUTERNAME, oops,

93
00:04:43,140 --> 00:04:46,540
I can't spell COMPUTERNAME properly today,

94
00:04:46,540 --> 00:04:50,330
so let's execute this. I now end up with a

95
00:04:50,330 --> 00:04:52,390
new job that's being created, so I can say

96
00:04:52,390 --> 00:04:55,790
$job, and you'll see it's a background job

97
00:04:55,790 --> 00:04:57,720
called Job5. Now, remember, it has

98
00:04:57,720 --> 00:04:59,530
different numbers to the ones that are

99
00:04:59,530 --> 00:05:01,830
locally when I choose Get‑Job because

100
00:05:01,830 --> 00:05:05,340
Get‑Job is local to the specific instance.

101
00:05:05,340 --> 00:05:07,900
So now that we have this, how do I get the

102
00:05:07,900 --> 00:05:10,310
results back from that? I mean, I can keep

103
00:05:10,310 --> 00:05:13,980
executing job until it has a status of

104
00:05:13,980 --> 00:05:16,140
whatever it would be. This is where we

105
00:05:16,140 --> 00:05:21,480
obviously issue another command which will

106
00:05:21,480 --> 00:05:24,980
retrieve the value that we're looking for.

107
00:05:24,980 --> 00:05:26,440
So, first off, I'm going to say

108
00:05:26,440 --> 00:05:30,090
Invoke‑Command into the current $session,

109
00:05:30,090 --> 00:05:31,330
and then I'm going to pass my

110
00:05:31,330 --> 00:05:34,470
‑ScriptBlock. And in my ‑ScriptBlock here,

111
00:05:34,470 --> 00:05:37,840
what we're going to do differently is I'm

112
00:05:37,840 --> 00:05:41,900
going to say Receive‑Job. And then, of

113
00:05:41,900 --> 00:05:43,610
course, what we get is the instance and

114
00:05:43,610 --> 00:05:46,380
the ID. I know that it was 5 because that

115
00:05:46,380 --> 00:05:48,890
was the one that was just created. So I

116
00:05:48,890 --> 00:05:51,830
now have my Invoke‑Session, and I can

117
00:05:51,830 --> 00:05:53,480
execute that. And, sure enough, it

118
00:05:53,480 --> 00:05:54,850
instantly gives me the name, which is

119
00:05:54,850 --> 00:05:58,400
OFFICE, which is my machine name. So I've

120
00:05:58,400 --> 00:06:01,450
now interrogated a remote job that's

121
00:06:01,450 --> 00:06:04,810
running on a machine and then received the

122
00:06:04,810 --> 00:06:07,600
data that came back. So if we go back

123
00:06:07,600 --> 00:06:09,680
through this process again, but this time,

124
00:06:09,680 --> 00:06:10,680
let's use something a bit more

125
00:06:10,680 --> 00:06:13,640
complicated. So let's go back to our

126
00:06:13,640 --> 00:06:16,080
Get‑Process. So I'm going to initiate that

127
00:06:16,080 --> 00:06:19,510
job. I'm going to go and get $job, which

128
00:06:19,510 --> 00:06:22,120
is ID of number 7. I'm then going to go

129
00:06:22,120 --> 00:06:25,280
and initiate another command here with 7.

130
00:06:25,280 --> 00:06:27,240
And now I get that information from the

131
00:06:27,240 --> 00:06:30,340
remote machine. So the Receive‑Job option

132
00:06:30,340 --> 00:06:33,090
allows you to retrieve the information

133
00:06:33,090 --> 00:06:37,000
results from that job irrelevant of whether it's local or remote.

