WEBVTT
00:00:00.500 --> 00:00:06.620
[Music]
00:00:17.900 --> 00:00:18.730
>> I was going to eat all of these
00:00:18.850 --> 00:00:20.900
Oreo cookies before we went
back on so that I had Oreo
00:00:21.020 --> 00:00:23.420
cookie stuff in my.
Yeah, okay.
00:00:23.720 --> 00:00:24.690
>> Yeah, no.
>> It was...
00:00:24.690 --> 00:00:25.290
>> No. No.
>> ...not good.
00:00:25.340 --> 00:00:28.710
>> Error handling. Because I never
make an error, this one's all
00:00:28.760 --> 00:00:31.690
yours, babe, because I
don't make mistakes.
00:00:31.740 --> 00:00:32.240
>> Well.
00:00:33.230 --> 00:00:39.090
>> Yeah, clearly if you've watched
any of this...
00:00:39.140 --> 00:00:41.360
00:00:41.400 --> 00:00:41.900
[Laughter]
00:00:41.970 --> 00:00:45.470
>> Oh, that's good. So, yeah, if you've
been watching any of this,
00:00:45.520 --> 00:00:50.700
I constantly make mistakes and I constantly,
when I'm using commandlets,
00:00:50.750 --> 00:00:54.700
am going to make mistakes. And it's
really great to get a helpful
00:00:54.750 --> 00:00:58.880
or useful error message, and it's
also really great to trap or
00:00:58.930 --> 00:01:02.220
grab hold of an error message that
doesn't make any sense and
00:01:02.270 --> 00:01:05.780
give me one that does make more
sense to me. And what we wanted
00:01:05.830 --> 00:01:10.150
to do was do some basic error handling.
Now this can go very
00:01:10.200 --> 00:01:13.300
deep with the error handling. And
what I've done is set up some
00:01:13.350 --> 00:01:17.100
simple examples of doing some basic
error handling to get you started.
00:01:17.350 --> 00:01:19.540
And so let's take a look at this
and see what you think.
00:01:19.590 --> 00:01:22.710
First of all here's what we're going
to do, we're going to show
00:01:22.760 --> 00:01:27.110
you how Power Shell treats things
when errors happen, and I think
00:01:27.160 --> 00:01:29.570
this is really cool so this is
going to be fun little demo.
00:01:29.620 --> 00:01:32.670
And then, we're going to show you
how to start to trap when errors
00:01:32.720 --> 00:01:37.270
happen and then how to deal with them and
something called a try-catch-finally.
00:01:37.730 --> 00:01:42.150
And I've got a couple of examples.
Obviously, I have something
00:01:42.200 --> 00:01:44.260
that's a better example; you know
I'm going to start with a lousy
00:01:44.310 --> 00:01:47.820
one apparently and then, I'll have
a better one and we'll see
00:01:47.870 --> 00:01:48.890
what goes from there.
00:01:49.570 --> 00:01:52.160
Please, join in because I'm going
to screw this up. It's all
00:01:52.210 --> 00:01:53.070
about errors. That's all I...
00:01:53.120 --> 00:01:53.620
Yeah.
00:01:54.640 --> 00:01:55.560
>> Yeah, it's awesome.
00:01:55.610 --> 00:01:58.600
>> Here's the deal: dollar sign
ErrorActionPreference. This is
00:01:58.650 --> 00:02:02.280
one the interesting things. This
kind of freaks people out in
00:02:02.330 --> 00:02:06.290
Power Shell at first that when they
run something it just does it.
00:02:06.340 --> 00:02:13.280
And to me: that's what I want;
don't ask me, "Are you sure?"
00:02:13.330 --> 00:02:17.610
Yeah, I was sure when I typed it
in; just do it. And Power Shell
00:02:17.660 --> 00:02:23.140
does that. But that's actually controlled,
though. The ErrorActionPreference
00:02:23.190 --> 00:02:26.730
of getting errors and how errors
get handled as it goes through
00:02:26.780 --> 00:02:29.820
and it does things is something
that's controlled. And we have
00:02:29.870 --> 00:02:34.290
this ErrorActionPreference variable.
What is this ErrorActionPreference
00:02:34.340 --> 00:02:35.910
variable thingy?
00:02:35.960 --> 00:02:40.660
>> Yeah, so basically the issue
is when something goes wrong,
00:02:40.710 --> 00:02:42.740
what is the right thing
to do for the user?
00:02:43.460 --> 00:02:46.630
And the real answer is
who the heck knows?
00:02:46.680 --> 00:02:49.330
Right? It could be this, it could
be that. It could be that.
00:02:49.380 --> 00:02:52.980
So what we do is we have a default
answer and then, we allow
00:02:53.030 --> 00:02:56.160
the user to decide to do something
differently. Well, let me
00:02:56.210 --> 00:03:00.760
give you an example. As a
developer you write code.
00:03:01.420 --> 00:03:04.270
There's always this issue of the
compiler. Should the compiler
00:03:04.320 --> 00:03:07.880
generate an error on the first
error it sees and then stop?
00:03:08.800 --> 00:03:11.110
And some of them used to work
that way and say, "Oh, okay.
00:03:11.160 --> 00:03:13.880
Great. I got an error." So I go fix that
error. Then, you compile again.
00:03:13.930 --> 00:03:15.150
It's like next line.
00:03:15.200 --> 00:03:16.060
>> Next line.
00:03:16.110 --> 00:03:19.620
>> Here's the error. It's like, okay,
fine. Fix that. Next line.
00:03:19.670 --> 00:03:22.440
And it's like, "Hey, you know what?
Why don't you give them all
00:03:22.490 --> 00:03:27.320
to me and then I'll fix them all
at once?" And so sometimes you
00:03:27.370 --> 00:03:30.300
want it that way. Sometimes it's
like, hey, I'm going to go do
00:03:30.350 --> 00:03:34.220
something to a very large set of
systems but if something goes
00:03:34.270 --> 00:03:39.520
wrong, I've got to stop and then
roll it on back because it's
00:03:39.570 --> 00:03:43.120
all got to be perfect. And other
times it's like what do I know?
00:03:43.170 --> 00:03:45.510
I think everything is going to be
fine; I'm not sure what to do.
00:03:45.560 --> 00:03:47.540
If something goes wrong, come back
and tell me and then, we'll
00:03:47.590 --> 00:03:51.460
discuss it. And so that's what we
do is we allow you to choose
00:03:51.510 --> 00:03:55.300
what behavior you want to have. And
in fact, the story is a little
00:03:55.350 --> 00:03:58.900
more complicated than that. I wish
I had a whiteboard here to
00:03:58.950 --> 00:04:00.050
describe it. But in fact...
00:04:00.100 --> 00:04:01.810
>> You can use my hat and you
can just draw on that.
00:04:01.860 --> 00:04:04.840
>> Well, you know I've described
this. In Power Shell
00:04:06.690 --> 00:04:10.850
it looks like a command line: A
piped to B piped to C. It looks
00:04:10.900 --> 00:04:13.870
like a traditional pipeline, but
that's not all the case.
00:04:13.920 --> 00:04:17.340
It's actually masking some amazingly
powerful technology.
00:04:17.710 --> 00:04:22.600
And what happens is it's an actual
object flow engine where A
00:04:22.650 --> 00:04:29.130
B C are these compute units that then
get hooked up in this directed graph.
00:04:29.180 --> 00:04:32.180
Notice I didn't say acyclic
graph. Directed graph.
00:04:33.710 --> 00:04:37.920
And we pass control to one and
it runs. We output an object.
00:04:37.970 --> 00:04:42.070
We then give that to the Power
Shell runtime engine who looks
00:04:42.120 --> 00:04:45.460
at the data contract required by
the next guy. And then, the
00:04:45.510 --> 00:04:49.040
data engine does all this manipulation.
"Hey, can I just accept
00:04:49.090 --> 00:04:53.030
it as is? Do I have to parse it
and match the properties?
00:04:53.450 --> 00:04:57.490
Do I have to do any coercion or
casting or parsing required to
00:04:57.540 --> 00:05:00.450
give it to it?" And then, it hands
it to it and then it runs.
00:05:00.500 --> 00:05:03.180
>> Which is the magic about making
the pipeline work which is...
00:05:03.230 --> 00:05:03.870
>> That is the magic.
00:05:03.920 --> 00:05:07.770
>> We got into it from an administrative
level, but that's definitely deeper.
00:05:07.820 --> 00:05:10.440
But that is that magic that's
making that pipeline work.
00:05:10.490 --> 00:05:14.350
>> Exactly. Now the original design
was that the pipeline was
00:05:14.400 --> 00:05:20.130
just one of N streams of objects.
So you have this object and
00:05:20.180 --> 00:05:23.270
you have any of these N of
these streams. You got the
00:05:24.770 --> 00:05:27.760
output stream, the error stream,
the verbose stream, the warning
00:05:27.810 --> 00:05:31.410
stream, etcetera, and the idea was
that each one of these streams
00:05:31.460 --> 00:05:34.530
could have their own pipelines.
Now that was original design.
00:05:34.580 --> 00:05:37.960
It turns out that we didn't implement
it that way. But .
00:05:38.300 --> 00:05:38.800
[Laughter]
00:05:39.190 --> 00:05:43.370
I still owe who did that.
00:05:44.330 --> 00:05:45.440
>> Call Bruce.
00:05:45.490 --> 00:05:46.080
>> No, no.
00:05:46.860 --> 00:05:51.350
Anyway, but what we do is we do
treat it largely like that but
00:05:51.400 --> 00:05:53.780
then the actions... The point is,
is that ultimately what you'd
00:05:53.830 --> 00:05:56.530
like to be able to do is to say on
the error stream, "I can just
00:05:56.580 --> 00:06:00.480
run an entire other pipeline against
the error actions." Now
00:06:00.530 --> 00:06:03.930
we don't implement that. What we do
is we have a set of enumerated
00:06:03.980 --> 00:06:06.770
actions, and it's a little bit
simpler to understand.
00:06:06.820 --> 00:06:09.150
>> As a matter of fact, let's take
a look at some of those.
00:06:09.200 --> 00:06:11.080
So first of all, guys, this is
00:06:12.300 --> 00:06:15.060
the first demonstration I have in
here. I'm going to run these.
00:06:15.110 --> 00:06:16.040
>> We're in Mod 7.
00:06:16.090 --> 00:06:17.350
>> Yeah, Module 7.
00:06:18.310 --> 00:06:21.100
And these are the scripts for Module
7. And so, first of all
00:06:21.150 --> 00:06:24.300
we have the dollar sign ErrorActionPreference
variable which
00:06:24.350 --> 00:06:27.050
shows you what the default is. And
this is what Power Shell is
00:06:27.100 --> 00:06:29.500
going to do when it runs into an
error. The error is going to
00:06:29.550 --> 00:06:33.190
get displayed and he's going to
continue on. So let's take a
00:06:33.240 --> 00:06:36.550
look at what that looks like. I've got
a simple get-wmiobject win32...
00:06:36.600 --> 00:06:36.780
>> But wait.
00:06:36.830 --> 00:06:36.930
>> Oh, yeah, yeah.
00:06:36.980 --> 00:06:40.960
>> A bunch of people will never
remember this: the dollar ErrorActionPreference.
00:06:41.010 --> 00:06:44.210
So the way I remember this because
I don't remember this is you
00:06:44.260 --> 00:06:51.350
do a dollar sign variable... Sorry. Sorry,
do a dir space variable colon.
00:06:51.880 --> 00:06:53.480
>> Oh, okay. Variable colon.
00:06:53.530 --> 00:06:55.280
>> Star pref star.
00:06:55.330 --> 00:06:56.030
>> Star pref star.
00:06:56.080 --> 00:06:57.650
>> I know this...
Pref. Pref.
00:06:57.700 --> 00:06:58.070
>> Oh, pref.
00:06:58.120 --> 00:07:04.440
>> And you see that we have a set
of preferences and one of them
00:07:04.490 --> 00:07:07.790
is the ErrorActionPreference. So
that's how you can remember it.
00:07:07.840 --> 00:07:08.370
>> That's cool.
00:07:08.420 --> 00:07:10.610
>> I know that some variables somewhere
had something to do with
00:07:10.660 --> 00:07:13.690
errors or something to do with preferences.
Use wild cards and
00:07:13.740 --> 00:07:14.480
you'll be able to find it.
00:07:14.530 --> 00:07:16.850
>> And you'll be able to find it.
That's awesome. Now, guys,
00:07:16.900 --> 00:07:20.200
I want you to notice that we're
not going to change the system
00:07:20.250 --> 00:07:24.100
default right now. Instead we're going
to do it with the commandlets.
00:07:24.470 --> 00:07:27.370
Take a look at this get-wmi win32
computer name. I want you
00:07:27.420 --> 00:07:30.550
to notice, I'm not doing anything
special but I've got DC not
00:07:30.600 --> 00:07:34.590
online and Client. So you can pretty
much guess that there's
00:07:34.640 --> 00:07:37.810
going to be a failure. Notice what
Power Shell does when I run this.
00:07:37.860 --> 00:07:39.880
I'm going to clear the
screen down here.
00:07:39.930 --> 00:07:42.970
>> And by the way for you at home
you'll want to DC and Client
00:07:43.020 --> 00:07:44.250
to be like Local Host.
00:07:44.300 --> 00:07:45.660
>> Yeah, exactly. Oh, great.
00:07:46.350 --> 00:07:50.640
Yeah, great. That was a good point.
So let me go ahead and run
00:07:50.690 --> 00:07:54.310
it because it's all going to fail
if you do it . So let me run it.
00:07:54.360 --> 00:07:58.380
You see the first one gets executed.
So that was DC. We're trying
00:07:58.430 --> 00:08:01.610
to reach not online. We didn't
get it but, look. I want you
00:08:01.660 --> 00:08:04.510
to notice we got the first one.
We get an error message saying,
00:08:04.560 --> 00:08:08.560
"Hey, that server isn't available."
And then, it went and it
00:08:08.610 --> 00:08:12.950
did the next one. In other words, Power
Shell's default is to continue.
00:08:13.000 --> 00:08:15.860
So, "Here's the error but I'm going
to keep trying and I'm going
00:08:15.910 --> 00:08:19.290
to keep going." Now I want you
to see what some of the other
00:08:19.340 --> 00:08:23.320
options besides continue. Sometimes
when you're running this,
00:08:23.370 --> 00:08:26.520
well, I know there are some computers
out there that are not
00:08:26.570 --> 00:08:27.940
going to be available.
00:08:28.720 --> 00:08:31.090
And I don't want my screen bleating.
I don't want to see it
00:08:31.140 --> 00:08:34.630
on the screen. I just want it
to run. Well, take a look.
00:08:34.680 --> 00:08:39.680
We've got get-wmiobject win32 computer
name. Same problem, but
00:08:39.730 --> 00:08:42.730
now I'm going to use... And I've
used the abbreviated versions
00:08:42.780 --> 00:08:44.930
of this so let me show you what
the full versions are on the
00:08:44.980 --> 00:08:47.710
slide here first.
00:08:50.110 --> 00:08:54.510
We've got error action which can be
EA and we've got error variable.
00:08:54.560 --> 00:08:58.010
Error action means we can change
the action that Power Shell
00:08:58.060 --> 00:09:02.470
is going to do and we can store
the errors in error variable.
00:09:02.520 --> 00:09:05.070
Now if you're thinking about this,
"Well, if I store the errors
00:09:05.120 --> 00:09:06.170
in error variable...
00:09:06.750 --> 00:09:09.500
Oh, and I wanted to make an error
log. I could probably just
00:09:09.550 --> 00:09:13.600
out-file that variable." We'll
get there. So you can see that
00:09:13.650 --> 00:09:18.690
we're going to... So take a look.
I've got error action silently
00:09:18.740 --> 00:09:19.840
continue and
00:09:21.800 --> 00:09:26.780
EV my error. So I'm going run this
guy, and let me clear the
00:09:26.830 --> 00:09:29.490
screen so you can see what's
going to happen here.
00:09:33.730 --> 00:09:36.140
Now it's still pausing because it's
trying that computer that's
00:09:36.190 --> 00:09:40.900
not online. But look this time I
just got the results displayed.
00:09:40.950 --> 00:09:45.270
I didn't see any error messages.
But here's the best part: if
00:09:45.320 --> 00:09:50.580
I want the error messages, dollar
my error, it has the error
00:09:50.630 --> 00:09:52.720
message in it.
00:09:53.490 --> 00:09:56.060
And think about this because we're
going to do this in our script
00:09:56.110 --> 00:10:00.310
in a second. I want that error
in a log file. Well, pipe it
00:10:00.360 --> 00:10:04.970
out to file whatever you want to
call it, error log or whatever.
00:10:05.700 --> 00:10:08.640
So you can then quickly dump
the error out to a file.
00:10:09.650 --> 00:10:13.440
This is kind of cool. I like this
because it continues. Now this
00:10:13.490 --> 00:10:16.650
is not something... Oh by the way
I have to tell you that this
00:10:16.700 --> 00:10:21.370
is a Don Jones thing. He was trying
to get this to break because
00:10:21.420 --> 00:10:25.020
he was doing not online, and he couldn't
get any errors. It kept
00:10:25.070 --> 00:10:26.180
returning results.
00:10:27.010 --> 00:10:30.170
And he was at a client and he's
going DC not online client, and
00:10:30.220 --> 00:10:33.380
it kept coming back and it kept giving
him an answer for not online.
00:10:33.430 --> 00:10:35.500
And he's like, "What's going on?"
And one of the guys says, "We
00:10:35.550 --> 00:10:37.760
have a computer called
Not Online."
00:10:37.760 --> 00:10:38.760
[Laughter]
00:10:39.580 --> 00:10:41.000
>> Which apparently has no security...
00:10:41.000 --> 00:10:41.500
[Laughter]
00:10:41.600 --> 00:10:43.340
because it lets Don
Jones' query in.
00:10:43.390 --> 00:10:46.080
>> Yeah, because it's letting Don
Jones' query in. Let me show
00:10:46.130 --> 00:10:48.720
you another way, though.
And Jeffrey mentioned this: sometimes,
00:10:48.770 --> 00:10:49.770
though, when an error occurs...
00:10:49.820 --> 00:10:52.090
>> That's a way to set low
expectations at work.
00:10:52.100 --> 00:10:53.100
[Laughter]
00:10:53.630 --> 00:10:56.950
>> I know. Ping not online. Well, wait
a minute? Why are we calling
00:10:57.000 --> 00:11:00.000
it not online if it's online?
I don't get it.
00:11:00.940 --> 00:11:06.900
So Jeffrey mentioned this: another
thing we can do is maybe when
00:11:06.950 --> 00:11:11.070
an error does occur, we need to
go stop. Stop, stop, stop, stop
00:11:11.120 --> 00:11:13.740
because we're going to have to do a
rollback or something like that.
00:11:13.790 --> 00:11:16.420
So we can do an error action of stop.
00:11:17.120 --> 00:11:19.160
And as you can imagine... I'm going
run this. Let me clear the
00:11:19.210 --> 00:11:20.270
screen down here.
00:11:21.340 --> 00:11:22.810
Let me run it up here.
00:11:24.700 --> 00:11:28.660
It'll go through and what you're going
to see as soon as he errors
00:11:28.710 --> 00:11:31.640
out, notice he didn't do the third
computer; he stopped right
00:11:31.690 --> 00:11:33.890
away on this guy. Just stop.
00:11:34.800 --> 00:11:37.330
And now we can go on and
do something else.
00:11:38.230 --> 00:11:41.800
The last one that you can do for
error action is inquire.
00:11:41.850 --> 00:11:45.440
And this can be useful when you're
testing something and it's
00:11:45.490 --> 00:11:48.520
not working and you're trying to
figure out what's going on.
00:11:48.570 --> 00:11:52.500
Let me show you. I'm going to run
this one inquire. I usually
00:11:52.550 --> 00:11:55.260
use this when I'm trying to debug what's
going on with my commandlet
00:11:55.310 --> 00:11:58.520
or something like that. It comes
up and says, "Hey, look, I've
00:11:58.570 --> 00:12:01.250
got this error. What do you want
me to do? Do you want me to
00:12:01.300 --> 00:12:04.040
continue, not continue? Do you want
me to suspend?" Suspend is
00:12:04.090 --> 00:12:04.800
kind of cool, right?
00:12:04.850 --> 00:12:05.610
>> I love suspend.
00:12:05.660 --> 00:12:06.880
>> Because if I suspend...
00:12:07.830 --> 00:12:09.330
See this special prompt?
00:12:10.240 --> 00:12:13.470
The command is still there but anything
that I may have in variables
00:12:13.520 --> 00:12:16.550
and stuff, I can start to look
at it. I can start to inspect
00:12:16.920 --> 00:12:20.930
what's going on. This is actually
kind of cool except I forgot
00:12:20.980 --> 00:12:23.640
how to exit the suspend.
Is it just exit?
00:12:23.690 --> 00:12:27.370
>> Think type get, my friend.
Think type get.
00:12:27.420 --> 00:12:28.610
>> Think type get.
00:12:29.330 --> 00:12:35.030
Yes it is. See? Think type get.
So do I want to continue?
00:12:35.080 --> 00:12:37.850
Sure, I could continue and it'll
do the last one. So inquire
00:12:37.900 --> 00:12:39.950
can be really useful if you're
having an issue and you're not
00:12:40.000 --> 00:12:43.010
sure what's going on and you want
to kind of debug what's going on.
00:12:43.060 --> 00:12:48.030
Now I'm going to start to use some
of these so that we can start
00:12:48.080 --> 00:12:50.750
to do some error handling, and we're
going to start off with...
00:12:51.660 --> 00:12:53.260
>> Can we say... You didn't
show the cool stuff.
00:12:53.310 --> 00:12:55.570
>> What? What? I didn't show cool
stuff? I thought that was cool.
00:12:55.620 --> 00:12:56.980
>> Well, that's cool but you didn't
show the cool stuff.
00:12:57.030 --> 00:12:58.560
>> Oh, well then show
us the cool stuff.
00:12:58.610 --> 00:13:03.400
>> Okay. So let me show you this
one. So it turns out that on
00:13:03.450 --> 00:13:07.800
Windows there are no process ID's
that end in three. It's actually,
00:13:07.850 --> 00:13:12.060
I think, any odd number process ID's.
So you could GPS type 2...
00:13:13.240 --> 00:13:13.460
>> Seriously?
00:13:13.510 --> 00:13:19.740
>> Yeah. Type select, select ID. Notice
they're all even numbers.
00:13:21.020 --> 00:13:24.080
>> Okay. And the reason for that is?
I've never actually noticed this.
00:13:24.130 --> 00:13:25.580
>> Ah, well, that's just -inaudible-.
00:13:27.300 --> 00:13:30.760
Some good reason for it. I don't
know. So here's the deal.
00:13:31.500 --> 00:13:39.180
So you stop process. So ID 13.23.33.
Okay? And while I...
00:13:39.230 --> 00:13:40.300
You're going to get
three errors.
00:13:40.350 --> 00:13:40.530
>> Right.
00:13:40.580 --> 00:13:44.560
>> Okay, so that's the point is
that that's a safe thing to go
00:13:44.610 --> 00:13:48.930
experiment with, these error
actions. So you saw
00:13:50.890 --> 00:13:55.500
the error action, right, all that
good stuff. But now I want
00:13:55.550 --> 00:13:57.520
to take a second here on
this error variable.
00:13:57.570 --> 00:13:57.900
>> Okay.
00:13:57.950 --> 00:14:01.780
>> And I'm going to use E because
it's like T but it's E.
00:14:01.830 --> 00:14:04.510
>> It's like T but it's E because
it means error, right?
00:14:04.560 --> 00:14:06.380
>> So now what I have is E
00:14:07.850 --> 00:14:10.540
and I have all the errors. So
that's dollar sign E of zero.
00:14:11.830 --> 00:14:12.520
Now the point of this is...
00:14:12.570 --> 00:14:12.960
>> The first one, yeah.
00:14:13.010 --> 00:14:16.190
>> The first one, the second one.
The point of this is it's very,
00:14:16.240 --> 00:14:20.850
very rich. Now there's a trick to
this and it's a general Power
00:14:20.900 --> 00:14:24.620
Shell technique. So I'm going to
say, "Okay, well, I bet you
00:14:24.670 --> 00:14:29.560
there's more to it than that."
So I say pipe to get-member.
00:14:30.190 --> 00:14:33.450
And, oh, there's a ton of stuff.
Look at all this great stuff.
00:14:33.500 --> 00:14:36.050
I'm going to say, "Oh, well, I'd
like to see that." And so what
00:14:36.100 --> 00:14:38.240
I'm going to do is I'm going to
say dollar sign E is zero, and
00:14:38.290 --> 00:14:42.630
just do the first one, format list star
and it gives me the same thing.
00:14:42.680 --> 00:14:43.040
>> Yeah.
00:14:43.090 --> 00:14:47.870
>> Okay, so what's happening is, is
that the error object is treated
00:14:47.920 --> 00:14:49.510
specially by the host.
00:14:50.380 --> 00:14:53.340
Details don't matter. It's treated
specially by the host.
00:14:53.390 --> 00:14:57.880
In order to see all the rest of the
parameters of the properties,
00:14:57.930 --> 00:14:59.430
you have to say minus force.
00:15:00.590 --> 00:15:03.250
So this by the way is a general
Power Shell thing, and general
00:15:03.300 --> 00:15:08.360
Power Shell will do something. And
if you say, "No I really wanted
00:15:08.410 --> 00:15:11.880
to do this," you just say minus
force and it'll do it. So if
00:15:11.930 --> 00:15:15.890
you try and kill lsass...
Why don't you try that?
00:15:15.940 --> 00:15:16.690
>> Serious?
00:15:17.180 --> 00:15:18.160
>> It's your machine.
00:15:18.200 --> 00:15:19.200
[Laughter]
00:15:20.550 --> 00:15:27.400
Anyway, no, well I'll try it. Yeah,
yeah because I bet you it'll work.
00:15:27.450 --> 00:15:30.760
>> Oh, I like this. "I bet you
it'll work." Let's see.
00:15:31.700 --> 00:15:36.430
>> Get-process. Pipe to lsass.
I'm sorry, get-process lsass.
00:15:37.640 --> 00:15:39.530
Pipe to stop process.
00:15:40.120 --> 00:15:44.750
Now if this does not work, Jason
is taking over while I spend
00:15:44.800 --> 00:15:46.210
a bunch of time...
00:15:46.260 --> 00:15:47.710
>> Restarting your machine.
00:15:49.510 --> 00:15:51.830
>> And I wouldn't do this until...
By the way, don't do this
00:15:51.880 --> 00:15:53.380
in Power Shell Version 2.
00:15:54.040 --> 00:15:56.100
If you've got Version
2, don't do that.
00:15:56.150 --> 00:16:00.060
Okay. No, I think it'll work in
2; you should try it. So what
00:16:00.910 --> 00:16:01.390
happens is...
00:16:01.440 --> 00:16:02.550
>> Oh, great. Yeah,
yeah, yeah, yeah.
00:16:02.600 --> 00:16:05.800
>> Okay, and so what we did was...
By the way, because we did
00:16:05.850 --> 00:16:09.160
not used to do this. I did not
use to make these mistakes.
00:16:09.210 --> 00:16:12.010
So I said, "Hey, you know what?
We should do this." So what's
00:16:12.060 --> 00:16:15.340
happening here is it'll come
in and it'll say, "Oh,
00:16:16.910 --> 00:16:18.290
are you trying to terminate
00:16:19.570 --> 00:16:23.600
a process that you didn't
start?" >> Oh,
00:16:23.650 --> 00:16:23.960
okay.
00:16:24.010 --> 00:16:26.700
>> And if so it says, "Hey, that
doesn't sound right." >> Doesn't
00:16:26.750 --> 00:16:28.330
sound good.
00:16:28.380 --> 00:16:29.960
>> And by the way, let's make
sure I get this right.
00:16:30.010 --> 00:16:32.230
>> Get no. Is it no? No.
00:16:32.280 --> 00:16:35.440
>> Now if I wanted to, and I'm really...
What I'm going to do
00:16:35.490 --> 00:16:38.320
is I'm going put a pound sign
in front just in case.
00:16:38.370 --> 00:16:41.030
>> Oh, smart man. Yeah.
00:16:41.080 --> 00:16:43.450
>> So that's no matter what. And
what you do is you say, "No,
00:16:43.500 --> 00:16:46.340
I really want to have that do it."
You just say minus force.
00:16:46.980 --> 00:16:49.080
And then, we'll do that. We won't
tell you anything; we'll just
00:16:49.130 --> 00:16:55.050
go whack that thing. Okay, so now
back here you say, "Hey, I
00:16:55.100 --> 00:16:58.060
want to see these things. I really
want to see. So show it to
00:16:58.110 --> 00:16:59.410
me minus force."
00:17:00.790 --> 00:17:02.000
Oh, force.
00:17:02.780 --> 00:17:03.310
And you see...
00:17:03.360 --> 00:17:03.840
>> Oh there they are.
00:17:03.890 --> 00:17:06.200
>> ...we have all the details. Okay?
And there is some really
00:17:06.250 --> 00:17:11.770
cool stuff here including target
object. So target object 13.
00:17:11.820 --> 00:17:12.270
>> 13.
00:17:12.320 --> 00:17:14.900
>> So wait a second. So you mean
I could've said dollar sign E
00:17:14.950 --> 00:17:17.540
dot target object?
00:17:17.590 --> 00:17:21.130
>> Would it be E dot zero? Oh.
Oh. So you can get it for all
00:17:21.180 --> 00:17:21.440
of them.
00:17:21.490 --> 00:17:21.630
>> Yeah.
00:17:21.680 --> 00:17:23.360
>> Or you can do the individual
elements.
00:17:23.410 --> 00:17:26.910
>> So this is the point. So the point
is I'm going to do something,
00:17:26.960 --> 00:17:30.910
I'm going to perform an operation on
a thousand things, ten thousand
00:17:30.960 --> 00:17:34.650
things, thirteen of them fail. Well,
which thirteen? Well, good
00:17:34.700 --> 00:17:38.850
luck my friend. No, no, no. E captured
is an error variable.
00:17:38.900 --> 00:17:42.630
We have the target object and those
are the 13. And now I do
00:17:42.680 --> 00:17:48.080
a remediation action. You say dollar
sign for each... Well, what's
00:17:48.130 --> 00:17:50.960
short for E?
00:17:52.430 --> 00:17:56.250
I guess I'm kind of dumb.
00:17:56.300 --> 00:17:56.870
>> E?
00:17:56.920 --> 00:18:01.560
>> I guess I'll go back to T. For
each T and dollar sign E.
00:18:01.610 --> 00:18:05.720
This is not best practice by the
way. And then, you do something
00:18:05.770 --> 00:18:08.560
with it. So then you do
a remediation action.
00:18:09.240 --> 00:18:11.320
Sorry, E dot
00:18:12.610 --> 00:18:13.770
target object.
00:18:14.390 --> 00:18:17.200
>> And then you can do something
about it....
00:18:17.250 --> 00:18:19.540
>> Yeah, and then you do some action,
you know, restart it....
00:18:19.590 --> 00:18:22.160
>> That's interesting because I bet
you here in a moment you're
00:18:22.210 --> 00:18:26.440
going to help fix my code to be
even better like that because
00:18:26.490 --> 00:18:27.560
I did something else.
00:18:28.090 --> 00:18:31.040
>> But before that, you didn't
tell them... Are you going to
00:18:31.090 --> 00:18:32.840
tell them about the views?
00:18:33.610 --> 00:18:34.230
>> The views?
00:18:34.280 --> 00:18:34.830
>> The views.
00:18:34.880 --> 00:18:35.230
>> The views.
00:18:35.280 --> 00:18:36.330
>> Category view.
00:18:36.380 --> 00:18:36.840
>> Category view?
00:18:36.890 --> 00:18:38.570
>> You don't know about
category view?
00:18:38.620 --> 00:18:40.340
>> I don't think so. What
are you talking about?
00:18:40.390 --> 00:18:42.870
>> Okay, so again there's some error;
there's something like an
00:18:42.920 --> 00:18:46.590
error review or error... I don't
know. Anyway, so how do you
00:18:46.640 --> 00:18:48.700
find out? Dir variable...
00:18:49.690 --> 00:18:53.420
There's something about errors.
So let's just look. And so by
00:18:53.470 --> 00:18:57.040
the way this Maximum Error Count, like
don't screw that up or [Sound].
00:18:57.900 --> 00:19:00.650
So there's error view
and it's normal.
00:19:00.700 --> 00:19:02.270
>> It says normal view, okay.
00:19:02.320 --> 00:19:03.760
>> Oh, yeah, well that's
odd. Right?
00:19:03.810 --> 00:19:03.970
>> Yeah.
00:19:04.020 --> 00:19:07.570
>> So let's take a look. Let's say...
And by the way here's another
00:19:07.620 --> 00:19:10.540
cool trick. Dollar sign error
view... I don't know if it'll
00:19:10.590 --> 00:19:11.410
work on this one...
00:19:11.460 --> 00:19:16.580
equals... Oh, that's not good.
Anyway, so you got a no.
00:19:17.290 --> 00:19:21.090
Category view. Okay, so you just
got to know this one. And there
00:19:21.140 --> 00:19:23.590
are two views: normal view
and category view.
00:19:24.190 --> 00:19:29.060
So before I set that let's make
some errors. So error number
00:19:29.110 --> 00:19:31.160
one, stop process
00:19:32.510 --> 00:19:37.190
ID 13. Whoop. Don't do that.
00:19:38.240 --> 00:19:40.590
13 and some nonsense.
00:19:41.390 --> 00:19:43.270
And those are my errors.
00:19:44.130 --> 00:19:48.410
Now what I'm going to do is I'm going
to say dollar sign category view...
00:19:48.460 --> 00:19:50.540
sorry, dollar error view
00:19:52.070 --> 00:19:58.200
equals category view. And I'm
going to do them again.
00:20:02.620 --> 00:20:06.610
Look at that. So these
are very pithy ways...
00:20:07.160 --> 00:20:11.330
So instead, I mean, take a look.
Blah, blah, blah versus hey,
00:20:11.380 --> 00:20:15.080
object not found.
Int32. It's 13.
00:20:15.130 --> 00:20:16.090
>> It gives you the
value of what...
00:20:16.140 --> 00:20:16.500
>> There's the 13.
00:20:16.550 --> 00:20:17.030
>> ...it was doing.
00:20:17.080 --> 00:20:20.900
>> What was going on? I was doing a stop
process. What was the exception?
00:20:20.950 --> 00:20:22.350
Process command exception.
00:20:22.400 --> 00:20:25.600
>> You know, I like that and I
haven't seen that before.
00:20:25.650 --> 00:20:27.730
>> Here's the trick: it's always
been there my friend.
00:20:27.780 --> 00:20:28.370
>> Really?
00:20:28.420 --> 00:20:30.590
>> Yeah, look here. So
here's the text.
00:20:31.670 --> 00:20:32.980
Here's where it happened.
00:20:33.810 --> 00:20:35.900
Category info. It's right there.
00:20:36.580 --> 00:20:39.940
>> Oh, I see what you're saying.
So that view is just showing
00:20:39.990 --> 00:20:42.940
the terse category info
that's there.
00:20:42.990 --> 00:20:43.380
>> Yeah.
00:20:43.430 --> 00:20:44.480
>> Oh, that is awesome.
00:20:44.530 --> 00:20:46.860
>> Now here's the thing,
that thing there?
00:20:47.710 --> 00:20:52.810
It's not localized. It's going to
be the same everywhere as is
00:20:52.860 --> 00:20:55.950
this fully qualified error ID. Did
you ever pay attention to that?
00:20:56.000 --> 00:20:56.840
Do you know what that is?
00:20:57.490 --> 00:21:00.420
So look here. Let's do this fully
qualified... Let's set it
00:21:00.470 --> 00:21:03.350
back a second and run it again.
00:21:05.140 --> 00:21:06.800
So normal view.
00:21:07.980 --> 00:21:12.610
Let's do that. Stop process 13.
00:21:14.150 --> 00:21:18.040
Okay, this text is all going to
be localized, one language to
00:21:18.090 --> 00:21:18.370
the other.
00:21:18.420 --> 00:21:19.120
>> One language to the other.
00:21:19.170 --> 00:21:23.090
>> So imagine you're sitting there
in Poland and you run this,
00:21:23.140 --> 00:21:26.160
and you get this error message.
And you're like, "Oh, I wonder
00:21:26.210 --> 00:21:30.140
what that is." Well, I guarantee
if you take this Polish error
00:21:30.190 --> 00:21:32.830
text and put it into Bing,
it's going to say...
00:21:34.330 --> 00:21:35.780
>> Something back in
Polish, probably.
00:21:35.830 --> 00:21:36.080
>> I don't know.
00:21:36.130 --> 00:21:37.300
>> I don't know or .
00:21:37.350 --> 00:21:40.840
>> It's going to say, "I don't know."
But this is this fully qualified
00:21:40.890 --> 00:21:44.490
error ID. Now look what it says
process no process found for
00:21:44.540 --> 00:21:50.540
given ID comma and then a location.
So a specific error comma
00:21:50.590 --> 00:21:55.910
a location. And the goal of this
is you take this guy and you
00:21:55.960 --> 00:21:58.450
cut it and then, you go in here
00:21:59.770 --> 00:22:03.930
to Bing... the best search
engine in the world...
00:22:05.230 --> 00:22:06.220
and you paste it.
00:22:06.870 --> 00:22:11.260
And then, you find all the information
about that specific thing.
00:22:11.310 --> 00:22:15.010
So it acts like a unique search
engine optimization for all the
00:22:15.060 --> 00:22:17.560
content about an error you're going
to have. Again, you're going
00:22:17.610 --> 00:22:19.480
to have errors; you're going to
have errors all the time.
00:22:19.530 --> 00:22:21.880
And so how do you get help about
the errors? Well, the answer
00:22:21.930 --> 00:22:25.660
is sometimes we have help documentation
but the space of errors
00:22:25.710 --> 00:22:28.790
is pretty vast. In general what
you're going to find is that
00:22:28.840 --> 00:22:32.440
somebody else had this problem and
they've got some blog somewhere
00:22:32.490 --> 00:22:35.970
that describes what was going on
and then, this fully qualified
00:22:36.020 --> 00:22:38.410
error ID helps you find it.
00:22:38.460 --> 00:22:42.330
>> I love that. That really makes it
helpful then to find those errors.
00:22:42.380 --> 00:22:43.330
And you said it's
not localized.
00:22:43.380 --> 00:22:44.280
>> Yeah, it's never localized.
00:22:44.330 --> 00:22:47.550
>> So it's always going to look
just like that. So you put it
00:22:47.600 --> 00:22:49.870
into Bing and it rocks no
matter where you're at.
00:22:49.920 --> 00:22:53.340
>> Right. So that also means you
might find some Polish blogs
00:22:53.390 --> 00:22:56.710
about this. They might
not help you.
00:22:56.760 --> 00:23:00.790
>> Might help now. Yeah, I know.
Since I can't read it.
00:23:00.790 --> 00:23:01.290
[Laughter]
00:23:01.680 --> 00:23:03.340
>> You could send it
to a translator.
00:23:03.390 --> 00:23:06.600
>> I can send it to a translator.
Do we do that in Bing?
00:23:06.650 --> 00:23:06.810
>> Yeah, sure.
00:23:06.860 --> 00:23:07.480
>> Do we translate?
00:23:07.530 --> 00:23:08.000
>> Sure. Somewhere.
00:23:08.050 --> 00:23:08.910
>> Oh, awesome. Somewhere.
Okay.
00:23:08.960 --> 00:23:10.430
>> I don't. But someone does.
00:23:10.480 --> 00:23:13.530
>> Okay, folks, so take a look. We're
going to do a little error
00:23:13.580 --> 00:23:16.830
handling using this error action
and error variable, and I'm
00:23:16.880 --> 00:23:19.010
going to show you a simple way
to do this. And I want you to
00:23:19.060 --> 00:23:22.080
see this script that I have here.
This is the start, number
00:23:22.130 --> 00:23:25.460
2, and it's our comp info one. I'm
going to minimize the parts
00:23:25.510 --> 00:23:27.090
that we've already looked at.
00:23:28.150 --> 00:23:31.090
The switch error log to make an
error log, and I want you to
00:23:31.140 --> 00:23:34.680
notice that I've already got a
log file set up. It's C colon
00:23:34.730 --> 00:23:39.510
error log dot text. And so let
me go ahead and minimize him.
00:23:39.560 --> 00:23:42.780
My process section, take a look at
what I'm doing here. I'm going
00:23:42.830 --> 00:23:45.860
to take a bunch of computers and I'm
going to grab some information.
00:23:45.910 --> 00:23:49.890
I want some inventory information,
so I've got like three WMI
00:23:49.940 --> 00:23:52.880
commands here. And I'm going to
create an object. This is fine
00:23:52.930 --> 00:23:57.670
and dandy, but you know what's
a real, huge waste of time is
00:23:57.720 --> 00:24:00.800
if I'm hitting a computer that's
in that collection and it's
00:24:00.850 --> 00:24:05.030
not online; well, it's going to
be banging all these commands
00:24:05.080 --> 00:24:08.300
at it, trying it over and over
again. It's going to be slow.
00:24:08.350 --> 00:24:11.750
It's going to be trying to retry.
And then, when it fails on
00:24:11.800 --> 00:24:13.980
the first WMI command, what's it
going to do? It's going to go
00:24:14.030 --> 00:24:18.140
to the next command and it's going
to fail on that one. I know
00:24:18.190 --> 00:24:19.870
it's going to fail but it's going
to sit there and it's going
00:24:19.920 --> 00:24:22.670
to retry and it's going to retry.
And then, it's going to fail
00:24:22.720 --> 00:24:24.860
on the third one but it's going
to sit there and, you know, and
00:24:24.910 --> 00:24:29.320
retry it and retry it. What I'd
really like is, oh man, if you
00:24:29.370 --> 00:24:34.340
get an error, let's not do this
again. Let's not keep going
00:24:34.390 --> 00:24:38.180
through all of these. So take a
look. I've got something called
00:24:38.230 --> 00:24:42.920
a try-catch block. Now this one
of the ways that you can try
00:24:42.970 --> 00:24:46.450
and trap errors and then decide
to do something. So look at
00:24:46.500 --> 00:24:49.180
the simple format here for it.
I've got dollar sign computer
00:24:49.230 --> 00:24:51.980
equals not online; we know that
that doesn't work. So I've got
00:24:52.030 --> 00:24:55.180
this try-catch, and technically
it's try-catch-finally.
00:24:56.090 --> 00:24:59.100
And a lot of time finally
always executes.
00:24:59.150 --> 00:24:59.950
>> Always.
00:25:00.000 --> 00:25:02.100
>> A lot of times in a lot of stuff
that we're doing in Power
00:25:02.150 --> 00:25:05.540
Shell, you don't always see the
finally in here because we just
00:25:05.590 --> 00:25:08.890
don't need it. So if you take
a look I've got try...
00:25:08.940 --> 00:25:11.310
>> Just to be clear, so finally is
like when you want to do some
00:25:11.360 --> 00:25:14.990
clean up. You're doing something.
You've allocated some resources.
00:25:15.350 --> 00:25:18.770
And whether you succeed or fail,
you want to clean up. So that's
00:25:18.820 --> 00:25:19.920
what finally is for.
00:25:19.970 --> 00:25:22.820
>> And so if you're a developer, you
probably know the try-catch-finally
00:25:22.990 --> 00:25:26.230
really well. You can have multiple catches
that catch at different levels.
00:25:26.280 --> 00:25:30.160
We're not going there, but in Power Shell
it's a very powerful implementation.
00:25:30.210 --> 00:25:31.330
>> Oh, actually, you know what's
worth making a point?
00:25:31.380 --> 00:25:31.510
>> Oh, yeah, yeah.
00:25:31.560 --> 00:25:34.320
>> Sorry, this is completely off
topic. Completely off topic.
00:25:34.980 --> 00:25:37.560
So here we've been teaching you
this scripting and it just kind
00:25:37.610 --> 00:25:40.870
of popped into my head, this try-catch-finally,
there's this
00:25:40.920 --> 00:25:44.020
little secret thing going on. You've
been learning C sharp.
00:25:44.070 --> 00:25:48.030
You know, everything that you've
learned here by and large if
00:25:48.080 --> 00:25:50.900
you go pick up a C sharp book,
you're like, "Oh, look, these
00:25:50.950 --> 00:25:54.120
guys stole all this stuff
from Power Shell." >> Yes,
00:25:54.170 --> 00:25:54.300
they did.
00:25:54.350 --> 00:25:54.790
>> Yes, they did.
00:25:54.840 --> 00:25:59.950
>> Yes, they did. No, in fact you'll
find it very, very similar.
00:26:00.000 --> 00:26:04.720
We chose to be very much match.
And if you know C, same thing.
00:26:05.380 --> 00:26:09.350
C stole from C plus plus which
stole from C and we stole from
00:26:09.400 --> 00:26:09.880
them all.
00:26:10.530 --> 00:26:13.300
And there are variations. Again,
I mentioned to you how Bruce
00:26:13.350 --> 00:26:17.040
and Jim and I struggled with this.
Hey, you know, if you just
00:26:17.090 --> 00:26:19.540
took any of those languages and tried
to make a shell out of it...
00:26:19.590 --> 00:26:23.360
I mean, people have and they're
really crappy shells. It turns
00:26:23.410 --> 00:26:27.410
out that that syntax just doesn't
work for an interactive shell.
00:26:27.460 --> 00:26:30.840
You need something different. And
so we struggled with that and
00:26:30.890 --> 00:26:34.580
we came up with what we think is
a good interactive shell but
00:26:34.630 --> 00:26:39.860
also plays homage to these lines
of programming languages.
00:26:40.190 --> 00:26:42.870
And so if you've learned Power Shell
and you decide, "Hey, I'd
00:26:42.920 --> 00:26:46.840
like to go on and do something
else," you'll find it a pretty
00:26:46.890 --> 00:26:50.560
smooth transition. It is different
but it's very comfortable.
00:26:50.610 --> 00:26:54.550
>> And I want to point that out that
a lot of research over many,
00:26:54.600 --> 00:26:59.080
many years went into admins doing
administrative stuff.
00:26:59.130 --> 00:27:03.750
Very verbose languages versus very
terse languages. And this
00:27:03.800 --> 00:27:07.600
really works well. C-based style,
this kind of thing, works so
00:27:07.650 --> 00:27:10.630
well for admins. And I just want
to reiterate what Jeffrey is
00:27:10.680 --> 00:27:13.880
saying: if you decide to start
moving towards, "Well, I want
00:27:13.930 --> 00:27:17.220
to do some more development and I
need Visual Studio," the language
00:27:17.270 --> 00:27:22.070
that you're going then for is C
sharp. You will be amazed at
00:27:22.120 --> 00:27:26.060
how much you already know because
Power Shell is written very
00:27:26.110 --> 00:27:29.940
similar to using that. And try-catch
is a great example because
00:27:29.990 --> 00:27:32.890
it's the same kind of try-catch
that they would be using in C
00:27:32.940 --> 00:27:36.570
sharp; it works the same way. So
take a look at my try here.
00:27:36.620 --> 00:27:40.150
What you do is you put your code
that you want to try to execute
00:27:40.200 --> 00:27:44.660
that you think might generate an error;
you put that in the try block.
00:27:44.710 --> 00:27:48.780
And notice what I've got here.
I've got 132 operating system.
00:27:48.830 --> 00:27:53.120
This dollar sign computer might fail,
so I put in an error action,
00:27:53.170 --> 00:27:54.300
"I want you to stop,"
00:27:55.580 --> 00:27:58.880
and then put the error into a variable
called current error.
00:27:59.380 --> 00:28:02.150
So I'm going to try that. Now here's
the important part to this,
00:28:02.200 --> 00:28:03.230
you need to stop.
00:28:04.570 --> 00:28:08.940
That's what gets caught so that
we can go to the catch block.
00:28:08.990 --> 00:28:13.080
If you said continue, well it's
going to continue. It's not
00:28:13.130 --> 00:28:16.780
going to go to the catch block.
The idea here is, "Try this and
00:28:16.830 --> 00:28:21.880
if it blows up, stop and let me catch
it and put up a nicer error,"
00:28:21.930 --> 00:28:25.610
in this case. Look at my catch.
I'm going to do write warning.
00:28:26.160 --> 00:28:29.490
I'm going to put up a slightly nicer
error message or a different
00:28:29.540 --> 00:28:33.240
error message. I could also do
some remediation code in here.
00:28:33.290 --> 00:28:36.630
Maybe I can try to test the connection
to the computer and see
00:28:36.680 --> 00:28:40.120
if it's really online or not. So
let me just run this for you
00:28:40.170 --> 00:28:42.300
so you can see what's going to
happen. I'll clear the screen
00:28:42.350 --> 00:28:43.110
down here.
00:28:48.950 --> 00:28:51.620
Now of course we're waiting for
it to fail because not online.
00:28:51.670 --> 00:28:54.860
But, look, warning. You've done made
a boo-boo with the computer.
00:28:54.910 --> 00:28:59.510
Not online. Yes? Cool? Is that kind
of cool? That's really cool.
00:28:59.560 --> 00:29:05.070
So it created a situation, stopped
and now I get to do my own thing.
00:29:05.120 --> 00:29:07.720
So the point of the try-catch block
is try what you think might
00:29:07.770 --> 00:29:11.400
cause a problem and then error
action stop and then, you can
00:29:11.450 --> 00:29:13.960
deal with it. The other cool thing
about this is, is I haven't
00:29:14.010 --> 00:29:17.110
lost the error. And even the cool stuff
that Jeffrey was showing us...
00:29:17.160 --> 00:29:21.350
dollar sign current error...
there's the error message
00:29:23.230 --> 00:29:26.570
because RPC service not available.
So I can now then put that
00:29:26.620 --> 00:29:31.010
into a file, however I wanted
to do it. In fact that's what
00:29:31.060 --> 00:29:34.520
we have right here under
the better try-catch.
00:29:35.690 --> 00:29:38.790
Notice I've got help here. This
is our function, comp info.
00:29:38.840 --> 00:29:42.200
I'm going to minimize the stuff that
we don't need. Take a look.
00:29:42.760 --> 00:29:47.750
I've got my try block and I'm trying
to make an object for each
00:29:47.800 --> 00:29:50.680
computer, so I've got all
of my codes in here.
00:29:51.770 --> 00:29:54.680
Whoops, well bridge too far. All
of my codes in here because
00:29:54.730 --> 00:29:57.990
I'm trying to make this object, and
I know the computer might fail.
00:29:58.040 --> 00:30:01.300
Well, I know that the first time
I might fail I certainly don't
00:30:01.350 --> 00:30:04.630
need to do the rest of this code.
So I'm going to go over here
00:30:04.680 --> 00:30:06.500
and go error action stop.
00:30:08.080 --> 00:30:11.590
I'll put the error into a variable.
Look at my catch block.
00:30:11.640 --> 00:30:15.190
Now I've decided that in my
catch block down here to...
00:30:16.590 --> 00:30:20.340
going to give you a warning but
if you turned on the switch,
00:30:20.390 --> 00:30:22.610
which we started out with this
morning, error log...
00:30:22.660 --> 00:30:24.270
>> Oh, I wondered where that would...
when that was coming....
00:30:24.320 --> 00:30:26.470
>> You wondered. I was going to
wrap it up towards the end.
00:30:26.520 --> 00:30:29.970
I'm an amazing kind of guy, rabbits
and all kinds of stuff.
00:30:30.020 --> 00:30:34.570
So if error log, so if you turn on
that switch parameter, here's
00:30:34.620 --> 00:30:36.520
what it's going to do: it's going
to take the date, put it out
00:30:36.570 --> 00:30:37.240
to a file.
00:30:37.750 --> 00:30:41.070
I've got a force so that if the
file already exists, it'll just
00:30:41.120 --> 00:30:44.810
overwrite it. It'll take the computer
name and it put it out
00:30:44.860 --> 00:30:47.520
to that file and then the current
error. Now the stuff that
00:30:47.570 --> 00:30:50.840
Jeffrey was showing us would also
be really useful, especially
00:30:50.890 --> 00:30:53.900
that target instance kind of
thing would be useful here.
00:30:53.950 --> 00:30:56.170
But let me show you how
this is going to look.
00:30:56.930 --> 00:31:02.100
So let me run this, clear the
screen. Oh, stop doing that.
00:31:05.250 --> 00:31:09.390
There we go. Down here. Clear
the screen. Get comp info.
00:31:10.750 --> 00:31:13.930
Computer name, not online.
00:31:15.250 --> 00:31:18.980
We'll do DC so you see that one works
and then, we'll do not online.
00:31:19.600 --> 00:31:22.250
Now if I don't put in the error
log switch, there won't be an
00:31:22.300 --> 00:31:24.540
error log. So I want an error log.
So I'm going to put in the
00:31:24.590 --> 00:31:28.020
switch parameter for error log
and let's see what happens.
00:31:28.070 --> 00:31:30.480
Well, there's DC. That's awesome.
That's great. I love it.
00:31:30.530 --> 00:31:34.430
Woot, woot, woot, woot. Oh, you
done made a boo-boo with the
00:31:34.480 --> 00:31:37.120
computer that's called
not online. Really?
00:31:37.170 --> 00:31:42.150
I turned on an error log so there
should be an error log out here.
00:31:42.200 --> 00:31:48.220
So let's see what the error log
has. Do I have... Oh, look,
00:31:48.270 --> 00:31:52.280
error log dot text. So I can
grab hold of that error log
00:31:53.550 --> 00:31:56.060
and now I've got the errors in there.
And if I was making multiple
00:31:56.110 --> 00:31:59.700
mistakes, it's getting appended, it
would be added in and added in.
00:31:59.750 --> 00:32:03.450
So this is a simple way to do error
handling but it gets you
00:32:03.500 --> 00:32:07.440
started down the road of being
able to trap errors when they
00:32:07.490 --> 00:32:11.670
occur and then do something about
the error and record it into
00:32:11.720 --> 00:32:15.380
a log file. So while it's simple,
this is actually a great place
00:32:15.430 --> 00:32:18.890
to start with your error handling
and working with it. Yeah?
00:32:18.940 --> 00:32:21.220
>> Yeah. That kind of log
file is kind of whack.
00:32:21.270 --> 00:32:24.280
>> Yeah, kind of whack.
But, yeah.
00:32:24.330 --> 00:32:26.260
>> Does write error write event log?
00:32:27.050 --> 00:32:30.470
>> But see that's the other cool
thing, right? So you guys know
00:32:30.520 --> 00:32:32.840
that you've seen us do
like get event log.
00:32:33.770 --> 00:32:37.910
Instead of writing to your own
log file, why not write to the
00:32:37.960 --> 00:32:38.890
real one?
00:32:38.940 --> 00:32:40.140
>> Yeah, show them that.
00:32:40.190 --> 00:32:40.590
>> So you have...
00:32:40.640 --> 00:32:42.800
>> Oh yeah we should tell... I think
we have a little bit of time.
00:32:42.850 --> 00:32:42.990
>> Yeah.
00:32:43.040 --> 00:32:45.810
>> We should show them that and then
tell them about the conversation
00:32:45.860 --> 00:32:46.730
that we had last night.
00:32:47.250 --> 00:32:47.810
>> Which one?
00:32:48.670 --> 00:32:50.720
Well, I was drinking so what
am I supposed to remember.
00:32:50.770 --> 00:32:52.520
Okay, so you remember that.
00:32:52.570 --> 00:32:52.700
>> Okay.
00:32:52.750 --> 00:32:54.860
>> So, guys, you've got this
commandlet, right?
00:32:54.910 --> 00:32:56.380
>> Yeah, show this.
This is good.
00:32:56.430 --> 00:33:04.080
>> Write...... Write event log,
log name, and you get to tell
00:33:04.130 --> 00:33:06.950
us where you want to write it.
So say you wanted to...
00:33:07.610 --> 00:33:08.360
>> Application.
00:33:09.000 --> 00:33:14.060
>> Application is a great place
for this to go. And I have to
00:33:14.110 --> 00:33:15.450
specify a source, right?
00:33:15.500 --> 00:33:18.910
>> Yes. And it is Jason
Saves You Money.
00:33:18.960 --> 00:33:23.080
>> Jason Saves You... It sounds like
an insurance commercial at
00:33:23.130 --> 00:33:23.580
this point...
00:33:23.630 --> 00:33:27.630
Money. What else do we need?
00:33:27.680 --> 00:33:29.220
>> Just say...
00:33:29.270 --> 00:33:29.620
>> No.
00:33:32.000 --> 00:33:35.340
>> No. Entry type information.
00:33:35.720 --> 00:33:36.740
>> Is information.
00:33:36.790 --> 00:33:37.440
>> Yeah, and then a message....
00:33:37.490 --> 00:33:40.270
>> You would put in like a script.
You'd probably do entry type
00:33:40.320 --> 00:33:40.850
of like error.
00:33:40.900 --> 00:33:41.710
>> Error, yeah.
00:33:41.760 --> 00:33:44.950
>> But this one is different. This
T's up -inaudible-. And then
00:33:45.000 --> 00:33:46.190
say minus message.
00:33:46.710 --> 00:33:47.460
>> Message.
00:33:47.720 --> 00:33:48.710
>> And then say quote
00:33:51.560 --> 00:33:54.030
script foo...
00:33:54.760 --> 00:33:54.990
>> Foo.
00:33:55.040 --> 00:33:55.760
>> Ran.
00:33:55.810 --> 00:33:56.160
>> Ran.
00:33:57.730 --> 00:33:57.870
>> Okay.
00:33:57.920 --> 00:34:00.650
>> So you could put this into your
script and of course fill in
00:34:00.700 --> 00:34:03.130
with variables for the message and
stuff like that. And so I'm
00:34:03.180 --> 00:34:07.070
going run this. Oh, event
ID; we forgot event ID.
00:34:08.250 --> 00:34:08.760
>> Seven.
00:34:08.810 --> 00:34:10.260
>> Seven. Sounds good to me.
00:34:12.330 --> 00:34:14.050
It didn't like the source name.
00:34:14.100 --> 00:34:15.930
>> Oh, yeah, you have to register
a source. Anyway so...
00:34:15.980 --> 00:34:16.870
>> You have to register
a source.
00:34:16.920 --> 00:34:19.530
>> ...it's pretty straightforward to
do but there's an extra step here.
00:34:19.580 --> 00:34:21.840
The point where we had this conversation
last night and Jason
00:34:21.890 --> 00:34:25.090
was telling me how when he and Don
train, they do training; they
00:34:25.140 --> 00:34:28.100
do awesome training. And they point
out that, hey, as you're
00:34:28.150 --> 00:34:31.980
learning Power Shell you're really
back to this point of like
00:34:32.680 --> 00:34:36.190
you do your job and you do your job
in a way that maximizes your
00:34:36.240 --> 00:34:40.580
integrated lifetime earnings. Now
in Rome, ancient Rome, they
00:34:40.630 --> 00:34:44.290
had this notion that in order
for justice to be done...
00:34:45.180 --> 00:34:47.930
it was not good enough for justice
to be done; justice must be
00:34:47.980 --> 00:34:52.690
seen to be done. Okay? So the public
awareness that justice was
00:34:52.740 --> 00:34:57.090
happening was just as crucial as
justice actually happening.
00:34:57.410 --> 00:35:03.190
So, too, saving time and saving
money is important. Being seen
00:35:03.240 --> 00:35:05.840
to save time and money
is just as important.
00:35:05.890 --> 00:35:06.150
>> Exactly.
00:35:06.200 --> 00:35:08.790
>> So they were talking about how,
hey, every time you write a
00:35:08.840 --> 00:35:12.860
script, you should document the
fact that, hey, this used to
00:35:12.910 --> 00:35:16.210
take this long and now with the
script it takes this long.
00:35:16.260 --> 00:35:18.340
And so we were brainstorming about
that. And then, he said,
00:35:18.390 --> 00:35:20.760
"Oh yes and at the end of the year,
you just go up to your boss
00:35:20.810 --> 00:35:24.800
and you say, 'Hey, by the way, ka-chunk,
here's what I've done
00:35:24.850 --> 00:35:29.130
and give me the money.'" No, just,
"Here's what I've done for
00:35:29.180 --> 00:35:32.260
you." And we thought, "Well geez,
how do you actually translate
00:35:32.310 --> 00:35:36.270
that from like I ran this script
and it saved this much time
00:35:36.320 --> 00:35:39.710
to what'd that save over the course
of the year?" And so we discovered
00:35:39.760 --> 00:35:43.510
this would be simple. If you just
wrote your script and every
00:35:43.560 --> 00:35:46.860
time you ran your script the first
thing you did was you first
00:35:46.910 --> 00:35:51.580
created the source, Jason Saves
You Money, and you logged the
00:35:51.630 --> 00:35:57.670
fact that it ran. Then what you
could do is say have a script
00:35:57.720 --> 00:36:01.040
that says, "Okay, I'm not going to
go across all these machines.
00:36:01.490 --> 00:36:04.760
I'm going to look in the application
event log for that event
00:36:04.810 --> 00:36:09.720
ID," you know the Jason Saves You Money,
the source and the event ID.
00:36:09.770 --> 00:36:13.830
And then, grab that all up, see
which script ran, have a table
00:36:13.880 --> 00:36:18.370
that said, "Oh this script saved this
much time," and then actually
00:36:18.420 --> 00:36:21.780
do a calculation and maybe even
do a chart. And do that on a
00:36:21.830 --> 00:36:25.890
weekly, a monthly, a yearly basis
to actually be super crisp
00:36:25.940 --> 00:36:29.370
about how much time scripting and
automation was going to save you.
00:36:29.420 --> 00:36:32.800
So anyway the point is it's actually
pretty trivial to do.
00:36:32.850 --> 00:36:35.120
>> And, well, the thing is... And
I have to talk to Don about
00:36:35.170 --> 00:36:37.160
this because this is actually
really cool. You're right.
00:36:37.210 --> 00:36:39.930
If I was writing this to the event
log that this script or this
00:36:39.980 --> 00:36:42.950
commandlet got executed that you've
built, what you're doing
00:36:43.000 --> 00:36:46.390
is you're showing and you have a
way to prove by using get-event
00:36:46.440 --> 00:36:47.880
log, by looking at that ID,
00:36:50.200 --> 00:36:53.400
how you're tool is being used that
you've created. This is good
00:36:53.450 --> 00:36:56.620
for informational purposes but
it's also good for you to say,
00:36:56.670 --> 00:37:03.460
"Hey look, I automated. I reduced the
cost of human hours down from...
00:37:03.510 --> 00:37:06.880
You know, I've saved you an entire
person." It sounds a little
00:37:06.930 --> 00:37:12.370
vindictive but it's time to eat your
peers. If they're not automating
00:37:12.850 --> 00:37:17.070
and you are, you're probably starting
to automate them out.
00:37:17.120 --> 00:37:20.240
You guys have seen this shirt, you
know, I think it goes something
00:37:20.290 --> 00:37:24.930
towards the fact of I can replace
you with a script. Well, yeah.
00:37:24.980 --> 00:37:26.850
And that's kind of what's happening.
00:37:28.010 --> 00:37:31.670
Writing it to the event log is a
great way to keep track of where
00:37:31.720 --> 00:37:34.830
it's been run, when it's been run,
how often it was used. I think
00:37:34.880 --> 00:37:37.960
that's a great measurement stick.
And that will definitely become
00:37:38.010 --> 00:37:40.130
part of the conversation now. That
was brilliant last night.
00:37:40.180 --> 00:37:43.600
You came up with that. That was
awesome. Well, guys, we've done
00:37:43.650 --> 00:37:47.020
some basic error handling for you
and you've got the examples.
00:37:47.070 --> 00:37:49.380
And I want to show you where we
are at in the slides because
00:37:49.430 --> 00:37:51.510
we're going to move on
a little bit here and
00:37:53.030 --> 00:37:57.190
take a look at dangerous commandlets.
So error action, catching
00:37:57.240 --> 00:37:58.420
errors with try-catch.
00:37:58.470 --> 00:37:59.490
>> Carlos dangerous.
00:37:59.540 --> 00:38:00.570
>> Carlos dangerous.
00:38:02.310 --> 00:38:04.740
A better example, I showed
you a better example.
00:38:04.790 --> 00:38:07.420
>> Commandlets to automatically tweet
to all of your followers.
00:38:07.470 --> 00:38:08.710
>> Oh, yeah. Don't, don't.
00:38:08.760 --> 00:38:10.380
>> Commandlets to automatically
tweet.
00:38:10.430 --> 00:38:11.810
>> Take pictures and tweet.
00:38:12.040 --> 00:38:13.850
>> No.
>> Oh god, that's terrible.