1
00:00:00,090 --> 00:00:01,860
Okay, so now we've understood how to get

2
00:00:01,860 --> 00:00:04,460
the status and the reason. One option

3
00:00:04,460 --> 00:00:06,780
that's available to us is the Debug‑Job

4
00:00:06,780 --> 00:00:09,530
command. This allows us to kind of walk

5
00:00:09,530 --> 00:00:11,790
through the scripts that are there. So the

6
00:00:11,790 --> 00:00:15,090
Debug‑Job command allows you to debug the

7
00:00:15,090 --> 00:00:17,790
scripts that are running within the jobs.

8
00:00:17,790 --> 00:00:20,340
So you can debug PowerShell workflow jobs,

9
00:00:20,340 --> 00:00:22,580
background jobs, and jobs that are running

10
00:00:22,580 --> 00:00:27,140
in remote sessions. Now, debug background

11
00:00:27,140 --> 00:00:33,230
jobs can be used to either debug remote

12
00:00:33,230 --> 00:00:37,340
background jobs, long‑running jobs, or

13
00:00:37,340 --> 00:00:40,030
also, jobs that use what's referred to as

14
00:00:40,030 --> 00:00:43,610
Wait‑Debugger. Now that's a specific job

15
00:00:43,610 --> 00:00:46,310
that in the script block has that command

16
00:00:46,310 --> 00:00:48,840
in the middle of it. So what this means is

17
00:00:48,840 --> 00:00:51,550
that any kind of jobs that we execute, we

18
00:00:51,550 --> 00:00:54,440
can actually debug and pause it and start

19
00:00:54,440 --> 00:00:57,470
it and kind of step through the job to see

20
00:00:57,470 --> 00:01:01,390
what it's doing. To debug a job, let's

21
00:01:01,390 --> 00:01:03,770
first create a job that uses the

22
00:01:03,770 --> 00:01:06,390
Wait‑Debugger. The reason for doing this

23
00:01:06,390 --> 00:01:08,060
is so that we can ensure that the

24
00:01:08,060 --> 00:01:11,550
Debug‑Job command can hook into it. So I'm

25
00:01:11,550 --> 00:01:13,890
going to create my script block upfront,

26
00:01:13,890 --> 00:01:16,300
which basically says, I have a variable,

27
00:01:16,300 --> 00:01:18,290
which is a value of 10. I'm going to

28
00:01:18,290 --> 00:01:19,830
output the value, and then I'm going to

29
00:01:19,830 --> 00:01:21,770
add 1 to it, and then I'm going to output

30
00:01:21,770 --> 00:01:24,180
that value again. But in the middle of

31
00:01:24,180 --> 00:01:26,270
this, before it does anything, it's going

32
00:01:26,270 --> 00:01:29,280
to sit and wait for the debug command to

33
00:01:29,280 --> 00:01:31,890
connect. Now, what I'm going to do is

34
00:01:31,890 --> 00:01:35,010
start that job. So job equals Start‑Job

35
00:01:35,010 --> 00:01:37,780
‑ScriptBlock and pass the scriptBlock. So

36
00:01:37,780 --> 00:01:39,520
that's going to initiate the job. Now if

37
00:01:39,520 --> 00:01:41,540
you retrieve the job at this point, it'll

38
00:01:41,540 --> 00:01:44,430
just say it's waiting. It's waiting for

39
00:01:44,430 --> 00:01:47,540
the debug to connect. Then, to connect the

40
00:01:47,540 --> 00:01:50,720
debugger, I'm going to say Debug‑Job ‑job

41
00:01:50,720 --> 00:01:53,510
and pass in that $job variable, which is

42
00:01:53,510 --> 00:01:56,660
the ID of the job that I created. At that

43
00:01:56,660 --> 00:01:59,950
point, I'll be able to then go through and

44
00:01:59,950 --> 00:02:02,560
iterate through by pressing the keyboard

45
00:02:02,560 --> 00:02:04,610
command. So, for example, C will be

46
00:02:04,610 --> 00:02:08,140
continue. I can step into, step out of,

47
00:02:08,140 --> 00:02:13,000
step over, and I can press the help to see what commands are available.

