1
00:00:00,510 --> 00:00:02,700
So let's talk about investigating failed

2
00:00:02,700 --> 00:00:06,220
jobs. Well, first off, a background job

3
00:00:06,220 --> 00:00:08,130
has to specific fields that we can

4
00:00:08,130 --> 00:00:10,150
utilize. The first one is the current

5
00:00:10,150 --> 00:00:12,740
status, so this will be started,

6
00:00:12,740 --> 00:00:15,640
completed, failed, etc., and then we have

7
00:00:15,640 --> 00:00:18,530
the actual reason of why something did or

8
00:00:18,530 --> 00:00:21,950
didn't work. Now to retrieve the current

9
00:00:21,950 --> 00:00:24,810
status, we first need to initiate a job

10
00:00:24,810 --> 00:00:27,030
that will fail. Now, for example, if I

11
00:00:27,030 --> 00:00:29,650
execute this one, Start‑Job‑ScriptBlock,

12
00:00:29,650 --> 00:00:32,980
Get‑EventLog, and the LogName is Nothing,

13
00:00:32,980 --> 00:00:36,470
then it's going to fail. It'll fail, one,

14
00:00:36,470 --> 00:00:39,140
because Get‑EventLog doesn't work in

15
00:00:39,140 --> 00:00:41,680
PowerShell 7, you have to use WMI log

16
00:00:41,680 --> 00:00:44,060
instead, or event, and then LogName

17
00:00:44,060 --> 00:00:46,130
Nothing doesn't exist, so either way,

18
00:00:46,130 --> 00:00:48,350
whatever PowerShell version we utilize, it

19
00:00:48,350 --> 00:00:51,370
will actually fail. What we can then do is

20
00:00:51,370 --> 00:00:54,270
look at the failed job and the child job

21
00:00:54,270 --> 00:00:55,930
details, because remember how this works,

22
00:00:55,930 --> 00:00:58,720
you get a job for the parent and a job for

23
00:00:58,720 --> 00:01:01,900
the ScriptBlock. So I choose Get‑Job and

24
00:01:01,900 --> 00:01:04,670
choose IncludeChildJob. This will then

25
00:01:04,670 --> 00:01:08,220
return the job details. I could also then

26
00:01:08,220 --> 00:01:11,750
just say, get the ChildJobState of Failed.

27
00:01:11,750 --> 00:01:14,270
So if I have multiple jobs, I can retrieve

28
00:01:14,270 --> 00:01:17,160
the ChildJob where it's all been failed.

29
00:01:17,160 --> 00:01:19,680
Now, of course, to retrieve the status and

30
00:01:19,680 --> 00:01:21,970
the reason, let's first look at retrieving

31
00:01:21,970 --> 00:01:24,590
the state information. So I can use a

32
00:01:24,590 --> 00:01:26,180
standard command that we've been using

33
00:01:26,180 --> 00:01:27,890
called GetDeskJob, I'm going to pass an

34
00:01:27,890 --> 00:01:30,690
Id, let's say it's called 1 for now, and

35
00:01:30,690 --> 00:01:32,400
then there's a property called

36
00:01:32,400 --> 00:01:36,390
JobStateInfo, and we can then expand this

37
00:01:36,390 --> 00:01:38,670
property. As with PowerShell, lots of

38
00:01:38,670 --> 00:01:41,180
properties contain the information, but it

39
00:01:41,180 --> 00:01:43,150
needs to be kind of expanded so you can

40
00:01:43,150 --> 00:01:45,680
read what's in there. So I can say

41
00:01:45,680 --> 00:01:49,040
JobStateInfo, go and get me that value.

42
00:01:49,040 --> 00:01:52,380
Now, the JobStateInfo property has lots

43
00:01:52,380 --> 00:01:54,260
more information inside of it, and other

44
00:01:54,260 --> 00:01:57,200
properties that we can also expand. So,

45
00:01:57,200 --> 00:01:58,910
for example, if I wanted to retrieve the

46
00:01:58,910 --> 00:02:03,000
reason of why a job failed, I can combine

47
00:02:03,000 --> 00:02:05,100
this together. I can say go and retrieve

48
00:02:05,100 --> 00:02:08,590
the job, expand the JobStateInfo, and then

49
00:02:08,590 --> 00:02:14,000
expand the property of Reason and it will then return the value that's expected.

