1
00:00:00,440 --> 00:00:03,620
Okay, so we've started, we've removed, and

2
00:00:03,620 --> 00:00:05,900
we've waited. Now how do we know what's

3
00:00:05,900 --> 00:00:07,700
going on, and what information can we

4
00:00:07,700 --> 00:00:11,210
retrieve from a background job? Well there

5
00:00:11,210 --> 00:00:13,310
are two options here, the first one is to

6
00:00:13,310 --> 00:00:16,380
call get‑job. This one gets the current

7
00:00:16,380 --> 00:00:18,370
PowerShell background jobs that are

8
00:00:18,370 --> 00:00:20,810
running in that current session. So in my

9
00:00:20,810 --> 00:00:23,270
PowerShell window, if I've initiated 10

10
00:00:23,270 --> 00:00:26,140
jobs, if I choose get‑job, it'll return

11
00:00:26,140 --> 00:00:28,610
those 10 jobs. The second one is

12
00:00:28,610 --> 00:00:33,420
receive‑job. This will get the results or

13
00:00:33,420 --> 00:00:35,830
the ending part of the PowerShell

14
00:00:35,830 --> 00:00:38,370
background job for that current session.

15
00:00:38,370 --> 00:00:41,030
So if my job was to retrieve a list of

16
00:00:41,030 --> 00:00:43,710
processes, if I choose get‑job, it'll give

17
00:00:43,710 --> 00:00:46,450
me the status. If I choose receive‑job, it

18
00:00:46,450 --> 00:00:50,730
will output the results for that job. So

19
00:00:50,730 --> 00:00:53,220
let's talk about using get‑job. If I want

20
00:00:53,220 --> 00:00:56,080
to retrieve the job details, let's say I

21
00:00:56,080 --> 00:00:58,610
initiate a start‑job, and it's going to

22
00:00:58,610 --> 00:01:01,200
get the processes, and I give it a name, I

23
00:01:01,200 --> 00:01:05,240
can then just say get‑job, name, and job

24
00:01:05,240 --> 00:01:06,770
because that's the name that I associated

25
00:01:06,770 --> 00:01:10,230
to it, and then that populates my job

26
00:01:10,230 --> 00:01:12,000
variable that I can then output with

27
00:01:12,000 --> 00:01:13,910
values, and it will give me the date, the

28
00:01:13,910 --> 00:01:16,830
time, the ID, the status, did it fail, did

29
00:01:16,830 --> 00:01:20,350
it complete, etc. I could also get child

30
00:01:20,350 --> 00:01:23,040
job details. Now you might not actually

31
00:01:23,040 --> 00:01:25,950
execute child jobs, but what happens is in

32
00:01:25,950 --> 00:01:28,890
the hierarchy of jobs, when you create a

33
00:01:28,890 --> 00:01:31,510
background job, it will create the initial

34
00:01:31,510 --> 00:01:35,080
one, and then it can spawn off child jobs

35
00:01:35,080 --> 00:01:37,700
to execute the various components under

36
00:01:37,700 --> 00:01:39,750
the covers. So for example, if we take the

37
00:01:39,750 --> 00:01:41,590
one at the top here that says Start‑Job

38
00:01:41,590 --> 00:01:43,820
and then Get‑Process, you'll actually end

39
00:01:43,820 --> 00:01:46,120
up with two jobs, a parent job and a

40
00:01:46,120 --> 00:01:49,440
child. The child job will be Get‑Process.

41
00:01:49,440 --> 00:01:52,070
So you can include the children, or the

42
00:01:52,070 --> 00:01:54,820
child processes in this one, and get a

43
00:01:54,820 --> 00:01:57,700
status of them. I could also get

44
00:01:57,700 --> 00:02:00,790
non‑started jobs, or failed, etc., by

45
00:02:00,790 --> 00:02:03,960
passing in the ‑state parameter and

46
00:02:03,960 --> 00:02:07,760
specifying a value. Now if I wanted to

47
00:02:07,760 --> 00:02:11,240
retrieve the results from a specific job,

48
00:02:11,240 --> 00:02:14,320
then of course we'll use receive‑job. Same

49
00:02:14,320 --> 00:02:16,670
process again. I'm going to start the job,

50
00:02:16,670 --> 00:02:19,040
Get‑Process to list me all the processes,

51
00:02:19,040 --> 00:02:21,500
and then I'm going to receive the job, and

52
00:02:21,500 --> 00:02:24,090
then that will output the value of that,

53
00:02:24,090 --> 00:02:26,640
whatever the results are supposed to be.

54
00:02:26,640 --> 00:02:28,440
We could also do this when we're looking

55
00:02:28,440 --> 00:02:30,650
at specific computers. So maybe I've got a

56
00:02:30,650 --> 00:02:32,940
domain controller, a SQL server, and a web

57
00:02:32,940 --> 00:02:35,010
server, and I want to be able to get the

58
00:02:35,010 --> 00:02:36,760
computer name, for example. So I'm using

59
00:02:36,760 --> 00:02:39,430
environment COMPUTERNAME. In this

60
00:02:39,430 --> 00:02:40,640
instance, I'm going to create a new

61
00:02:40,640 --> 00:02:43,260
PowerShell session, target these computer

62
00:02:43,260 --> 00:02:45,900
names, used the Invoke‑Command for those

63
00:02:45,900 --> 00:02:47,830
sessions, and then pass in the

64
00:02:47,830 --> 00:02:49,610
information. And then I can simply call

65
00:02:49,610 --> 00:02:53,280
get‑job, and this will then return the

66
00:02:53,280 --> 00:02:56,790
values for that one. Then I can pass in

67
00:02:56,790 --> 00:03:00,000
receive‑job to get the results when they've, once they've completed.

