►
Description
PowerShell Saturday is a training event for all things PowerShell. The event was held in Raleigh, North Carolina and hosted by Research Triangle PowerShell User Group.
Wouldn't it be nice to know when something breaks? Littlejohn will teach you how error handling works and different ways that you can use it in your scripts to find out when things go wrong.
The files for Littlejohns's slides and code can be found on our github page:
https://github.com/rtpsug/PowerShell-Saturday/tree/master/2019-NC.State/
A
A
So
about
me,
that's
my
name,
my
github
not
active
on
that.
Ever
my
Twitter
handle
and
I'm.
Also
the
head
for
the
Chattanooga
power,
show
user
group
and
a
little
bit
about
me.
I,
like
PowerShell,
like
automation,
I'm
an
IT
operations,
so
I've
done
everything
from
desktop
to
help
desk
to
if
it
management
to
tools
and
now
I'm
on
the
ServiceNow
project.
A
A
So
why
did
I
come
up
with
this
session?
There
was
a
real-life
event
that
there
was
a
table
introduction
that
no
longer
exists
a
to
this
day
and
we
take
a
knee.
We
pour
out
one
for
that
table
and
also
had
an
intervention
with
a
manager
about
that
incident.
So
with
that
aside,
I've
learned
from
that
and
now
I'm
retelling
why
we
should
be
doing
error
handling
and
what
it
is.
A
So
why
do
we
do
it?
We
do
it
because
our
scripts
may
not
always
work
I
support
a
homegrown
application
that
has
PowerShell
back
in
and
it
just
it's
a
one-stop
shop
for
the
helpdesk.
I
very
rarely
touch
the
system
except
when
it
breaks
and
the
reason
it
broke
is
because
everything
else
changed.
So
that's
why
I've
incorporated
error
handling
into
it
to
work
with
the
databases
down
or
you
know,
Knoxville
just
datacenter
just
went
down.
A
A
It'll
text
me
haven't
gotten
to
call
me
yet
if
y'all
can
figure
that
one
out,
let
me
know
because
I
want
my
scripts
to
call
me
as
well
and
it
emails
my
team
with
verbose
stuff
and
we
have
the
chance
once
we've
caught
the
error
to
actually
resolve
the
issue.
So
you
don't
even
have
to
log
in
on
Saturday.
A
So
what
is
error-handling
it's?
The
anticipation,
detection
and
resolution
of
a
problem.
I
took
those
out
of
a
long
definition
from
like
Webster
or
something,
but
we
need
to
be
ready
for
when
an
error
happens.
So
an
error
is
always
gonna
happen.
That's
why
we
have
you
know
on
call,
so
we
need
to
anticipate
this
even
happening
in
our
production
environment.
We
need
to
detect
it.
A
So
we
need
to
know
when,
where
why
we
need
to
have
a
transcript
of
what
happened
during
that
error,
and
we
need
to
try
to
resolve
it,
because
I
really
do
not
want
to
restart
a
service
when
we
have
start
service
or
restart
service
in
our
toolkit
and
just
the
obligatory
mean
eyebrows,
read
it
all
the
time
and
I
love
this
new
format.
For
this
mean
so
I
have
chosen
this,
and
this
is
I
didn't
edit
it
at
all.
It
came
straight
from
programming,
humor
or
something
so,
let's
not
be
little
kid.
C
A
A
No,
it
doesn't
exist,
don't
when
you
start
down
your
path
and
journey
into
PowerShell,
don't
get
scared
of
the
big
wall
of
red
text.
It's
really
trying
to
be
helpful.
It's
really
scary
at
first,
but
hopefully
at
the
end
of
this
session,
it'll
kind
of
be
able
to
pick
apart
what
all
this
means,
and
basically
it's
telling
for
beta
the
term.
A
So
the
the
de
facto
way
that
we
should
be
working
with
our
errors
is
a
try/catch
who
has
ever
written
a
try-catch
block
before
sweet
majority
of
ill
with
every
language
before
say
something
wrong.
I
want
to
believe
every
language
has
a
try-catch.
If
not
it
should
it's
the
simplest
way
to
handle
errors,
and
you
can
think
of
it
as
a
safety
net,
so
I'm
gonna,
let
my
script
run
in
production.
I'm
gonna
have
full
faith
that
my
script
is
not
gonna
break
anything.
A
However,
if
something
happens,
I
can
rely
on
this
trycatch
and
as
a
standard.
Try
to
keep
your
try
caches
small
when
I
first
got
to
the
in-house
application.
I
was
telling
you
about
the
guy
that
I
inherited
from
had
tried
caches
like
I,
had
to
scroll
pages,
and
it
was
just
bad.
Don't
do
that
your
try
should
have
a
single
action.
A
A
So
I've
taken
one
example
from
before,
and
all
I've
done
is
I've
just
inserted
it
into
the
try
block
and
I'm
gonna
write
out
some
output.
You
don't
have
the
correct
command,
let
maybe
you
know
who
does
and
I've
used
the
right
output
so
for
those
that
were
in
the
strings
session,
you
should
kind
of
have
a
back
history
on
the
right
output
and
what
has
happened
is
I
no
longer
have
my
big
wallet
for
a
text,
because
PowerShell
assumes
that
I've
already
handled
it.
It
doesn't
need
to
do
it.
For
me.
A
So
we've
got:
we've
got
the
idea
of
a
try-catch
we're
able
to
write
something
kind
of
thinking.
It
should
work,
but
we
know
whatever
happens
it's
going
to
catch
you,
but
what,
if
you're,
doing
checking
on
your
own
you're
testing
if
the
server's
up
you're
testing,
if
the
configurations
right
well
those
types
of
items,
don't
necessarily
have
errors
right
if
PowerShell
doesn't
know
to
error
out,
when
my
c
drive
has
less
than
60
gigs
or
it
doesn't
have
an
attached,
H
Drive
or
something.
So
what
can
we
do?
A
Well,
we
actually
can
force
a
catch
in
PowerShell.
We
can
say
PowerShell
if
something
it
goes
awry.
You
don't
exactly
know,
and
you
don't
want
the
script
to
go
forward.
You
can
actually
throw
that
and
the
key
word
is
throw
and
I
say
it's
a
key
word
because
we've
all
been
taught
PowerShell
as
verb
noun.
Well,
there
are
keywords:
throw
is
one
of
them
tries
one
of
them
for
each
things
like
that,
it
doesn't
really
follow
the
syntactic
rules.
Power
show
so
just
assume
it
as
a
keyword
and
it'll
make
it
good.
A
What
throw
does
is
it
creates
a
generic
error.
There's
nothing
special
about
it.
It
just
says
here's
an
error
of
boom
done.
You
can't
give
it
a
message.
Just
say
you
know.
Oh
no,
the
configuration
to
my
vCenter
lab
isn't
correct
and
then
it'll
be
caught
by
the
catch
block.
An
alternative
is
the
right
error
and
I
actually
have
an
example
later
on
talk
to
talk
about
the
right
error
and
for
a
use
case,
some
the
parameters
to
explain
those.
A
A
To
just
be
like
throw
this
message,
and
this
is
the
syntax
of
throat,
you
said
keyword,
you
give
it
a
message
and
it's
followed
up
with
a
catch.
They
catch
is
very
simple.
I
just
want
to
clear
the
host
from
all
this
stuff,
just
cleaning
up
and
right
out
to
the
screen
just
to
give
a
little
context
of.
What's
happening
so,
let's
go
ahead
and
run
that.
A
A
So
we
have
the
ability
to
catch
a
specific
error,
so
if
you've
ever
worked
in
try
catches,
some
of
the
languages
I
believe
someone
with
more
modern
languages.
Allow
you
to
have
multiple
catch
blocks,
so
you
have
one
try,
but
you
can
have
multiple
catches
depending
on
the
what
the
environment
looks
like,
depending
on
the
error.
If
the
file
doesn't
exist,
database
connections-
things
like
that,
so.
A
C
A
Of
like
a
switch
statement
where
there's
a
default
action-
and
that
is
what
lines
10
to
13-
is
that's
the
default
catch
reaction,
but
what
I'm
doing
is
I
want
to
go,
find
this
file
and
if
something
happens,
stop
taking
some
notes
from
the
previous
session.
I
gave
the
air
action
now
the
syntax
to
catch
is
specific.
Air
is
on
line.
Fourth,
you
just
attach
that
property
type
or
the
type
accelerator
to
the
end
of
the
catch
for
the
one
that
matches
the
error.
In
this
particular
example.
A
A
A
Gives
you
a
lot
more
flexibility
on
creating
the
air
so
throw
it's
a
generic
air?
Just
it
looks
really
ugly,
no
information
at
all.
You
can't
really
tell
what's
happening
with
it,
but
PowerShell
gives
you
the
ability
to
help
lower-tier
support,
understand,
what's
happening
to
your
scripts
and
so
I'll
go
through
them.
A
We
have
a
parameter
called
exception
and
it's
basically
more
of
the
details
of
you
know:
UD
peek,
firewall
connections
or
something
like
that
you
can
put
in
there.
You
have
the
parameter
called
message,
and
this
is
where
what
I
consider
giving
to
the
helpdesk
saying
they
probably
don't
understand
the
whole.
You
know
ISO
stack
of
a
network,
but
you
can
tell
them
hey.
This.
Is
a
network
issue
probably
go
to
the
networking?
You
can
give
it
a
category.
These
are
actually
predefined
error
categories.
That
PowerShell
gives
you
and
I
just
chose
connection
error.
A
A
C
A
It's
strictly.
Just
dollar
sign
error,
nothing
to
it.
It's
actually
an
array
of
objects,
so
we
can
catch
anything.
That's
in
your
sessions
will
be
in
this
error,
variable
and
you'll
just
index
into
which
one
you
need,
and
you
can
also
give
it
as
a
parameter
like
we
showed
before
in
the
preview
session.
The
error
variable
in
the
air
actually
can
work
together
to
name
it
something
different.
So
if
you
don't
want
to
throw
it
in
the
default
air,
you
can
give
it
your
own
air
variable.
A
A
A
But
the
discipline
of
the
error
message
can
be
a
little
bit
better
because
of
how
it
like
concatenates,
multiple
things,
the
exception,
the
category
ID
things
like
that,
so
going
back
with.
That
said,
we
can
treat
it
like
an
object
and
grab
each
one
of
those
from
it.
So
we'll
start
with
the
error,
category
and
0
index
will
get
you
the
last
error.
That
happened.
That's
really
good
when
you're
using
catch
blocks.
B
A
C
A
Were
we
expecting
suppose
we're
expecting
to
see
the
right
error
in
our
zero
index,
but
remember
what
I
said
before
it
remains
in
the
session
and
since
I've
never
closed?
This
is
e
during
the
presentation.
I
should
have
every
error.
That's
ever
occurred
in
my
demos
and
what
we're
assuming
with
negative
one
is
we
want
the
last
item.
That's
in
the
air
available,
correct,
okay,
so
here's
our
first
error
or
our
zero
index.
A
I
did
clean
it.
I
did
clear
it
recently.
Three,
that's
why,
okay!
So
if
you've
noticed
when
you
start
going
backwards,
you
can
only
go
as
far
as,
however
many
you
have.
However,
that's
on
an
error.
It's
just
kind
of
nature,
nature
of
the
beast
when
we're
working
with
it
I
only
have
three,
so
it
should
be
negative.
A
A
A
Okay,
so
now
that
I
have
enough
errors
in
there,
we
can
work
with
that.
So,
let's
find
the
category
information
for
actually
no,
let's
go
ahead
and
rerun
this,
so
I've
cleared
my
error
variable
again
and
so
I
want
to
run
and
get
the
category
information
now
power.
So
it's
telling
us
a
little
bit
of
the
category
information
which
is
which
I
told
it
was
the
right
error.
A
A
So
we've
got
our
category
information
that
matters
in
your
environment
feel
free
to
use
it
everything
in
power
shells
for
y'all
to
use.
So
let's
look
at
the
exception.
This
is
the
extracted
issue,
so
the
read
text
doesn't
really
tell
you
anything.
You
can
always
go
back
to
Don
exception
and
it
will
tell
you
what
your
issue
is.
A
C
A
Ps
invocation
information,
I
think
it
will
tell
you
anything.
You
would
ever
want
to
know
about
the
execution
of
that
script.
Well,
in
this
context,
it
will
tell
you
the
execution
information
of
the
error
where
it's
located,
what
script
it's
in
and
things
like
that,
and
it
is-
has
plenty
plenty
of
properties.
A
Really
good
stuff
here,
because
I'm
using
the
IFC
and
I'm
using
like
inline
scripts.
A
lot
of
this
information
is
going
to
be
either
null
or
blank
or
empty
arrays
things
like
that
and
even
just
line
number
0,
but
when
you
use
it
inside
scripts
or
files,
and
things
like
that,
it
gives
you
lots
of
information
about
where
the
error
was.
What
line,
and
it
also
tells
you
the
offset.
So
the
offset
is
how
many
characters
over
it
was
zero
being
at
the
very
beginning
to
20
or
30
or
40.
A
A
It'll
even
tell
you
this
back
trace
and
it's
a
lot
better
than
the
Java
stack
traces
that
was
in
Java,
but
it
helps
us
narrow
down.
Here's
our
file
I
know
exactly
what
the
path
of
it
is.
I
know
what
line
the
error
happened
and
if
we
go
up
line,
it--
line
18
just
so
happens
to
be
where
my
right
air
is.
So
if
you
start
at
line
18
and
work
our
way
back
up
to
figure
out
what
the
issue
is.
B
A
A
A
And
I
told
a
promise:
Joe
there's
an
easy
way
to
find
specific
errors
or
that
little
type
accelerator
and
that
one
actually
is
kind
of
it
was
they
don't
put
in
the
best
place
as
possible?
They
put
it
in
the
most
inconvenient
place,
they
put
it
inside
of
a
property
and
that
property
has
a
method.
And
then
you
take
that
method
and
you
get
that
property.
A
A
Writing
error
and
everything
like
that.
So
what
I'm
wanting
to
do
here
is
utilize.
More
of
the
customer
error.
Custom
error
variables
I'm,
actually
going
to
speak
on
the
air
action
on
why
I
specifically
set
it
here
in
the
next
couple,
slides
so
I'll
glance
over
that
so
I'm
doing
is
I'm
gonna
find
that
file
that
doesn't
exist
and
I'm
setting
any
error
to
the
error
variable
because,
for
whatever
reason,
I
don't
like
the
default
error.
Variable
I'm
also
catching
that
specific
error,
because
I
found
it
through
the
pre
full
name
here.
A
C
B
C
A
B
A
That's
why
I
wrote
it
like
this,
so
one
of
the
things
that
we
lose
when
we
start
using
the
error
variable
is
that
it's
no
longer
an
exception,
like
the
error
variable
that
we're
used
to-
and
we've
done,
demos
on
through
this
whole
session
is
of
the
type
exception.
Your
variable
here
is
just
a
variable.
It's
to
be
used
elsewhere
and
I've
had
to
find
that
out
live
so
so
what
is
actually,
in
my
custom,
air.
C
B
A
A
I'm
still
an
object
here
we
go
so
what
I
told
you
a
second
ago
was
kind
of
wrong
it
when
it
does
actually
work.
It
will
actually
give
you
these
items
as
if
it
was
an
error.
Variable
I
apologize
from
is
speaking
there,
but
we'll
continue
on
with
this
being
the
truth.
So
I've
got
my
category
information
I've
got
my
exception,
I'm
able
to
get
a
custom
error
and
use
it
later
on
I'm
able
to
one.
A
A
A
And
actually
it's
in
my
gotcha
Lars,
you
pointed
out
what
was
in
my
gotcha
line.
It
actually
prevents
the
correct
use
of
the
error
variable
for
when
you,
when
PowerShell
works
like
it's
supposed
to
and
who
understands
or
who's
ever
used.
The
dollar
sign
underscore,
probably
in
your
where
objects
all
the
time
back.
In
version,
2
version,
3,
they've
introduced
PS
item,
which
is
supposed
to
be
there.
The
actual
dollar
sign
underscore,
but
never
really
caught
on.
B
A
A
A
The
actual
issue
here
is
that
yip-yip
process
and
when
you
can't
find
it
is
not
a
terminating
error
like
there's
a
lot
of
processes
that
don't
exist,
infinity.
Basically
so
yes,
it's
an
error.
I
can't
complete
the
the
use
of
get
process,
but
it
didn't
really
hinder
what
happened
and
because
it
wasn't
a
terminating
error.
The
catch
block
never
ran,
and
so
going
back
to.
The
point
made
is
that
the
solution
is
the
error.
Action
needs
to
be
set
to
stop,
because
you
need
to
tell
PowerShell
you
need
to
be
a
terminating
error.
A
A
We're
duplicating
efforts
which
doesn't
really
matter
here.
Most
people
are
scared
of
duplicating
efforts,
but
whenever
an
error
happens,
it
always
goes
to
the
error
variable
when
the
error
happens,
but
you
can
also
basically
tee
it
off
and
put
it
in
your
own
variable.
The
error
is
in
both
locations.
A
A
A
A
C
A
A
D
A
The
question
is:
is
there
a
way
to
find
out
what
commandlets
are
terminating
versus
non
terminating
I
haven't
researched
that
so
I
guess
that
would
be
homework
yeah,
but
the
reason
I
don't
have.
The
answer
is
I.
Really
just
did
the
trial
and
error
like
what
is
which
you
Charlotte
errand
Deb's,
not
production,
so
play
with
it.
Power
shows
there.
A
You
can
use
confirm
as
a
parameter
just
to
make
sure
or
what,
if
just
to
make
sure
you're
not
really
breaking
anything,
but
that's
just
outside
of
the
topic
of
air
handling,
so
go
out
there
play
with
it
figure
out.
What's
there,
the
trial
and
error
is
why
I
made
this
session
good
questions.
Anything
else.
D
A
A
And
I'm
gonna
give
credit
where
credit's
due,
and
that
was
a
Jetix
thing
that
I
saw
and
so
when,
if
I
accidentally
hit
f5,
it's
gonna
be
like
no.
No
because
most
of
my
one-on-one
stuff,
it's
a
long
script
and
it's
line
by
line
working
through
it
right
but
other
than
standards.
You
could
probably
follow.