WEBVTT

00:00:02.050 --> 00:00:03.860
So let's go back into our environment,

00:00:04.000 --> 00:00:07.460
and we'll first look at how we retrieve and query event logs.

00:00:08.150 --> 00:00:11.260
And then lastly, we'll export the current event log.

00:00:12.940 --> 00:00:18.130
Now one of the most common things that you'll often do is to retrieve event log

00:00:18.140 --> 00:00:21.990
information or query the logs for specific types of things.

00:00:22.560 --> 00:00:24.610
So there are a couple of commands that we can utilize.

00:00:24.610 --> 00:00:30.970
The first one is Get‑EventLog, which is a command.

00:00:30.980 --> 00:00:35.300
Now you'll notice in PowerShell 7, when I execute this, it fails.

00:00:35.310 --> 00:00:38.530
So let me go here and type PowerShell,

00:00:38.540 --> 00:00:40.760
and I'll use the Windows PowerShell option.

00:00:41.090 --> 00:00:42.570
Let's just launch that here.

00:00:44.070 --> 00:00:51.150
And then I'll say Get‑EventLog, and this time it works.

00:00:51.160 --> 00:00:54.860
So there's a difference between the supportability of the two commands.

00:00:54.870 --> 00:00:58.700
Get‑EventLog is going to be pre‑PowerShell 7,

00:00:58.710 --> 00:01:01.440
whereas the other one, Get‑WinEvent,

00:01:01.450 --> 00:01:03.400
is going to be obviously in the newer version.

00:01:03.410 --> 00:01:06.810
But what you can see, it's going to ask me for the name,

00:01:06.810 --> 00:01:07.910
so I'm going to say system.

00:01:07.910 --> 00:01:09.510
And I'll stop that scrolling,

00:01:09.510 --> 00:01:12.670
but that's all that the system commands inside the log.

00:01:12.670 --> 00:01:19.000
If I go back to PowerShell 7 here and say Get‑WinEvent,

00:01:19.010 --> 00:01:21.790
I can press Enter here, it comes back and says whoa,

00:01:21.790 --> 00:01:22.100
whoa, whoa!

00:01:22.100 --> 00:01:25.380
There's too much information that you're trying to bring back.

00:01:25.390 --> 00:01:29.130
So we obviously have specific properties that we can utilize.

00:01:29.140 --> 00:01:33.920
So I'm going to say system and stop that scrolling because it goes too fast,

00:01:33.930 --> 00:01:36.540
but you'll see it groups all this information together,

00:01:36.550 --> 00:01:38.820
and you can see it scrolling on the screen.

00:01:38.830 --> 00:01:44.030
So, a fairly straightforward way of retrieving specific values.

00:01:44.400 --> 00:01:49.070
Now of course, if we wanted to only bring specific things in,

00:01:49.080 --> 00:01:53.380
then we would utilize, if we're using Windows PowerShell 7,

00:01:53.390 --> 00:01:55.490
so we would use Get‑WinEvent,

00:01:55.620 --> 00:02:00.070
and then what we can do is create a FilterHashtable,

00:02:00.080 --> 00:02:03.610
which effectively is a list of properties or key‑value pairs.

00:02:03.620 --> 00:02:07.540
So the first thing here is I'm going to say LogName,

00:02:07.550 --> 00:02:10.419
because obviously I want to specify the log name.

00:02:10.800 --> 00:02:15.370
And what we'll do here is we'll do the Application log

00:02:15.380 --> 00:02:19.880
and we can do the System log as well.

00:02:19.880 --> 00:02:23.630
So we can do multiple at the same time, so Application and System.

00:02:25.150 --> 00:02:29.820
I can then determine the levels that I wish to bring back.

00:02:29.830 --> 00:02:31.130
When we talk about the levels,

00:02:31.130 --> 00:02:34.640
that will be the types of events that we're trying to bring back.

00:02:34.650 --> 00:02:38.640
And then maybe I want to set a start time.

00:02:39.300 --> 00:02:45.260
So, let's say I wanted to say get the current date,

00:02:45.990 --> 00:02:52.580
and maybe I want to add some hours, basically taking some off,

00:02:52.580 --> 00:02:53.730
do Enter.

00:02:53.820 --> 00:03:03.470
And then that creates me a query that filters everything to note time and date,

00:03:03.480 --> 00:03:07.500
12 hours previous, and only for those types of events,

00:03:07.510 --> 00:03:10.340
which would be 2 and 3, which are going to be Warning,

00:03:10.340 --> 00:03:11.400
as you can see here,

00:03:11.420 --> 00:03:14.250
is coming through and anything that would be kind of critical.

00:03:14.260 --> 00:03:18.690
So depending on the level that you choose will determine what gets returned.

00:03:18.860 --> 00:03:23.560
But it's really quick and easy to retrieve values from

00:03:23.570 --> 00:03:25.900
the event log using that format.

00:03:26.490 --> 00:03:27.360
Now of course,

00:03:27.370 --> 00:03:33.080
you can always do it the other way by just looping through the properties.

00:03:33.090 --> 00:03:35.770
So you can see Credentials, FilterXPath, Force.

00:03:35.780 --> 00:03:38.340
So how does that work when we want to do it remotely?

00:03:38.460 --> 00:03:43.750
What we can do here is you could specify a ComputerName if needed.

00:03:43.750 --> 00:03:48.420
So I could say 192.168.127.128,

00:03:48.660 --> 00:03:54.110
and then I could say Credential and do cred and press Enter.

00:03:54.410 --> 00:03:57.520
Now what this will do is depending on the session that's been created,

00:03:57.560 --> 00:04:00.980
you will be able to connect to that machine,

00:04:00.990 --> 00:04:05.210
pass in those credentials, and then it will retrieve any values that come back.

00:04:05.310 --> 00:04:08.390
The other approach, which is a cleaner option,

00:04:08.400 --> 00:04:13.420
is to actually create the PowerShell session first and then go and

00:04:13.430 --> 00:04:17.019
execute the commands in the context of that one there.

00:04:17.329 --> 00:04:19.800
Now of course mine tells me the RPC server is unavailable.

00:04:19.880 --> 00:04:21.040
That's perfectly fine.

00:04:21.320 --> 00:04:24.460
If you look, I still have a session available.

00:04:24.560 --> 00:04:31.920
So if my connection isn't working, I can go in and say Enter‑PSSession,

00:04:33.600 --> 00:04:39.310
access the session, like so, and then I can say Get‑WinEvent,

00:04:39.320 --> 00:04:41.590
let's say system, and Enter.

00:04:41.600 --> 00:04:45.570
And those are all the values from the other machine,

00:04:45.580 --> 00:04:47.760
the other server, the other client.

00:04:47.770 --> 00:04:52.840
Or Windows or Linux or even macOS we can retrieve those same kind of values.

00:04:52.850 --> 00:04:58.710
I'm just going to exit that PSSession, like so.

00:04:58.710 --> 00:05:02.840
Now of course, that's obviously fairly straightforward,

00:05:02.840 --> 00:05:06.350
but obviously in the grand scheme of things you often want

00:05:06.350 --> 00:05:11.050
to connect to multiple servers or you want to filter

00:05:11.050 --> 00:05:12.920
specific types of information,

00:05:12.930 --> 00:05:17.060
and that can be done very simply either in PowerShell

00:05:17.060 --> 00:05:19.400
7 or in the Windows PowerShell.

00:05:19.400 --> 00:05:20.290
It really depends.

00:05:20.290 --> 00:05:21.090
It's your choice.

00:05:21.100 --> 00:05:24.470
If we go to the blue option here, which is the Windows PowerShell,

00:05:24.480 --> 00:05:26.210
I'm just going to clear this for a second,

00:05:26.210 --> 00:05:30.380
we can then use the previous version of the command,

00:05:30.380 --> 00:05:33.550
so I can say Get‑EventLog, I'm going to say system.

00:05:33.550 --> 00:05:36.550
And then you'll see I've got various options,

00:05:36.550 --> 00:05:39.750
so I could say After a specific date, Before,

00:05:39.760 --> 00:05:41.750
UserName, InstanceId, etc.

00:05:41.860 --> 00:05:46.160
Or what I could do is just say go to the system log and then I can use

00:05:46.160 --> 00:05:51.160
standard PowerShell commands to filter the values.

00:05:51.330 --> 00:05:58.520
So I could say $_.EntryType.

00:06:00.760 --> 00:06:04.460
I could say match an Error.

00:06:04.970 --> 00:06:10.790
So that would give me a list of all the error options that were available.

00:06:10.850 --> 00:06:15.230
Now of course I could change that and say Warning,

00:06:15.230 --> 00:06:16.940
and that would give me all the warning ones.

00:06:16.950 --> 00:06:18.200
And you get the point.

00:06:18.200 --> 00:06:19.320
We can obviously change.

00:06:19.320 --> 00:06:22.490
Now, obviously in Get‑WinEvent I was using the level numbers,

00:06:22.500 --> 00:06:26.120
but here, in the original one, in Windows PowerShell,

00:06:26.130 --> 00:06:27.270
I can use the names.

00:06:27.280 --> 00:06:31.500
So it's a very simple process to be able to get information out

00:06:31.510 --> 00:06:35.700
of the Windows event logs simply by just running standard queries

00:06:35.710 --> 00:06:39.670
using Get‑WinEvent in PowerShell 7, passing in what you're looking for,

00:06:39.680 --> 00:06:42.800
and then being able to query those specific properties.

00:06:44.810 --> 00:06:48.150
Now one of the last things that we might want to do after querying

00:06:48.150 --> 00:06:51.470
everything is actually export those values out.

00:06:51.480 --> 00:06:58.780
So if we go back to our Get‑WinEvent, let's use the properties,

00:06:58.910 --> 00:07:07.730
so LogName system, and then we can say Select‑Object.

00:07:07.740 --> 00:07:10.440
I'm just going to do * here because I basically want to

00:07:10.440 --> 00:07:12.530
bring back every property that's there.

00:07:12.540 --> 00:07:15.450
From here I can then pipe that value again,

00:07:15.460 --> 00:07:24.020
say Export‑Csv, and then at this point I can just generate a specific file.

00:07:24.020 --> 00:07:32.100
So I can say ExportedEvents.csv, like so.

00:07:33.050 --> 00:07:35.140
So now this will go ahead and output that.

00:07:35.140 --> 00:07:37.320
So you can see I'm in C:\Users\Trainer.

00:07:37.330 --> 00:07:44.710
So if I launch here C:, Users, Trainer, you'll see there's an exported CSV.

00:07:44.720 --> 00:07:45.760
If I click Edit,

00:07:45.810 --> 00:07:50.470
you can see all of my values have now been dropped directly into that file.

00:07:50.520 --> 00:07:54.380
So we can very simply export anything by combining that.

00:07:54.390 --> 00:07:57.590
If I wanted to have specific headers and columns,

00:07:57.690 --> 00:08:02.070
then I would reconstruct the Select‑Object to get the right values and

00:08:02.070 --> 00:08:04.790
then put extra properties here on the export to.

00:08:04.950 --> 00:08:09.790
But that's as easy as it is to get the values out and dumped down to a CSV file,

00:08:09.790 --> 00:08:10.430
for example.
