Okay, so now we've understood how to get
the status and the reason. One option
that's available to us is the Debug‑Job
command. This allows us to kind of walk
through the scripts that are there. So the
Debug‑Job command allows you to debug the
scripts that are running within the jobs.
So you can debug PowerShell workflow jobs,
background jobs, and jobs that are running
in remote sessions. Now, debug background
jobs can be used to either debug remote
background jobs, long‑running jobs, or
also, jobs that use what's referred to as
Wait‑Debugger. Now that's a specific job
that in the script block has that command
in the middle of it. So what this means is
that any kind of jobs that we execute, we
can actually debug and pause it and start
it and kind of step through the job to see
what it's doing. To debug a job, let's
first create a job that uses the
Wait‑Debugger. The reason for doing this
is so that we can ensure that the
Debug‑Job command can hook into it. So I'm
going to create my script block upfront,
which basically says, I have a variable,
which is a value of 10. I'm going to
output the value, and then I'm going to
add 1 to it, and then I'm going to output
that value again. But in the middle of
this, before it does anything, it's going
to sit and wait for the debug command to
connect. Now, what I'm going to do is
start that job. So job equals Start‑Job
‑ScriptBlock and pass the scriptBlock. So
that's going to initiate the job. Now if
you retrieve the job at this point, it'll
just say it's waiting. It's waiting for
the debug to connect. Then, to connect the
debugger, I'm going to say Debug‑Job ‑job
and pass in that $job variable, which is
the ID of the job that I created. At that
point, I'll be able to then go through and
iterate through by pressing the keyboard
command. So, for example, C will be
continue. I can step into, step out of,
step over, and I can press the help to see what commands are available.