So let's talk about investigating failed
jobs. Well, first off, a background job
has to specific fields that we can
utilize. The first one is the current
status, so this will be started,
completed, failed, etc., and then we have
the actual reason of why something did or
didn't work. Now to retrieve the current
status, we first need to initiate a job
that will fail. Now, for example, if I
execute this one, Start‑Job‑ScriptBlock,
Get‑EventLog, and the LogName is Nothing,
then it's going to fail. It'll fail, one,
because Get‑EventLog doesn't work in
PowerShell 7, you have to use WMI log
instead, or event, and then LogName
Nothing doesn't exist, so either way,
whatever PowerShell version we utilize, it
will actually fail. What we can then do is
look at the failed job and the child job
details, because remember how this works,
you get a job for the parent and a job for
the ScriptBlock. So I choose Get‑Job and
choose IncludeChildJob. This will then
return the job details. I could also then
just say, get the ChildJobState of Failed.
So if I have multiple jobs, I can retrieve
the ChildJob where it's all been failed.
Now, of course, to retrieve the status and
the reason, let's first look at retrieving
the state information. So I can use a
standard command that we've been using
called GetDeskJob, I'm going to pass an
Id, let's say it's called 1 for now, and
then there's a property called
JobStateInfo, and we can then expand this
property. As with PowerShell, lots of
properties contain the information, but it
needs to be kind of expanded so you can
read what's in there. So I can say
JobStateInfo, go and get me that value.
Now, the JobStateInfo property has lots
more information inside of it, and other
properties that we can also expand. So,
for example, if I wanted to retrieve the
reason of why a job failed, I can combine
this together. I can say go and retrieve
the job, expand the JobStateInfo, and then
expand the property of Reason and it will then return the value that's expected.