WEBVTT

00:00:00.150 --> 00:00:05.460
So let's look at how we use the Get‑CimInstance and Invoke‑CimMethod commands

00:00:06.090 --> 00:00:08.760
to kind of enhance the process that we're going through.

00:00:08.760 --> 00:00:12.770
Well the Get‑CimInstance command retrieves the CIM

00:00:12.770 --> 00:00:15.370
instances of the class that we've chosen.

00:00:15.470 --> 00:00:17.370
So in the previous example, we looked at,

00:00:17.370 --> 00:00:22.750
we were saying Win32_ something, so OperatingSystem,

00:00:22.750 --> 00:00:24.060
ComputerSystem, etc.

00:00:24.700 --> 00:00:29.640
This supports either the class name, or you can actually query if you wanted to.

00:00:29.730 --> 00:00:32.860
So in the previous examples, we wrote the class name itself.

00:00:32.860 --> 00:00:36.970
This will return one or more CIM instance objects,

00:00:36.970 --> 00:00:40.140
and it will use the Windows Management Instrumentation,

00:00:40.140 --> 00:00:46.110
or WMI, using a component object model locally to retrieve that information.

00:00:46.140 --> 00:00:49.290
It will then use what's called the CIM server if it's a

00:00:49.300 --> 00:00:52.200
remote connection using a session.

00:00:52.210 --> 00:00:54.010
So let's just clear this again.

00:00:54.080 --> 00:00:59.170
Let's say we wanted to view the operating system information.

00:00:59.170 --> 00:01:01.790
So we can use our same command, but this time,

00:01:01.790 --> 00:01:07.210
instead of it being Get‑CimClass, I want to get an instance of that class name.

00:01:07.460 --> 00:01:12.740
So the CIM class object allows me to retrieve details about the class; the CIM

00:01:12.740 --> 00:01:16.330
instance gives me everything around the specific class name,

00:01:16.330 --> 00:01:20.360
which is connected to the instance or the machine that we're talking to.

00:01:20.540 --> 00:01:27.250
So I can say Win32, and I'll say OperatingSystem here and then press Enter.

00:01:27.250 --> 00:01:31.430
And you'll see, it's given me the instance of the class name,

00:01:31.430 --> 00:01:35.130
but specifically to the build of the machine that I'm actually running it on.

00:01:35.140 --> 00:01:36.890
So this is a Windows 10 device.

00:01:36.900 --> 00:01:41.000
If I was to run this from a Windows Server 2016,

00:01:41.000 --> 00:01:45.980
2019, 2022, etc., I would get different values back for those machines.

00:01:46.800 --> 00:01:47.130
Now of course,

00:01:47.130 --> 00:01:51.500
I can also just get information from specific computers

00:01:51.500 --> 00:01:53.880
too by passing the ComputerName object.

00:01:53.890 --> 00:01:56.850
So I'm going to say WIN10, I can press Enter,

00:01:56.850 --> 00:02:00.520
and it will give me the values back for the same machine that I'm on.

00:02:00.540 --> 00:02:03.350
If I say 2022 and enter it,

00:02:03.630 --> 00:02:06.450
you'll notice that it gives me a bit of an error here,

00:02:06.450 --> 00:02:09.430
which is all to do with the WinRM client.

00:02:09.449 --> 00:02:11.710
So what we can do is just clear this a second.

00:02:11.720 --> 00:02:13.870
I can go back and take that same command.

00:02:13.870 --> 00:02:17.900
And I'm just going to do the IP address, and I'll explain why in a second.

00:02:17.940 --> 00:02:22.130
So we'll do 128, press Enter, and it goes and gets me the values.

00:02:22.280 --> 00:02:25.680
It's all because I can't resolve the names because they're separate machines,

00:02:25.680 --> 00:02:29.640
and it's all to do with trusted host, but we'll talk about remoting another time.

00:02:29.770 --> 00:02:33.720
But this allows me to retrieve the values from those machines.

00:02:34.020 --> 00:02:37.580
So that's how we get the instances of the classes using CIM class,

00:02:37.580 --> 00:02:42.330
and then we get the values associated to the instance of the class that

00:02:42.330 --> 00:02:45.800
we've chosen for the machine that we're calling into.

00:02:45.800 --> 00:02:50.840
Now, we can get more than just the operating system or the computer system, etc.

00:02:50.850 --> 00:02:54.910
If I do Get‑CimInstance, I'm going to take away the ComputerName here,

00:02:54.920 --> 00:03:00.880
we can change this to just be ComputerSystem like so,

00:03:00.880 --> 00:03:05.540
and you'll see I get different information than the operating system.

00:03:05.560 --> 00:03:10.000
If I also want to see a list of services that may be running on the machine,

00:03:10.000 --> 00:03:13.710
I can say Win32_Service, and it gives me the same information,

00:03:13.710 --> 00:03:17.540
albeit it looks slightly different to using Get‑Service,

00:03:17.540 --> 00:03:19.200
which is the regular PowerShell command.

00:03:19.710 --> 00:03:23.450
Now we also have the ability, so let me just clear this here.

00:03:23.610 --> 00:03:28.180
We also have the ability to start filtering that information out as well.

00:03:28.340 --> 00:03:35.040
So let's say I wanted to identify services that are running on a machine,

00:03:35.040 --> 00:03:36.990
so I've got my class Win32.

00:03:36.990 --> 00:03:42.940
But I also want ones that are not set to run as the local system account.

00:03:42.950 --> 00:03:48.150
So I'm looking for services effectively that have been set to specific accounts.

00:03:48.160 --> 00:03:50.750
Maybe it's domain/user or something else like that.

00:03:50.760 --> 00:03:53.990
So I can come into here and use a Filter parameter,

00:03:53.990 --> 00:03:57.490
and then I can use kind of standard syntax.

00:03:57.490 --> 00:04:01.140
In this one, I'm going to say startname,

00:04:01.140 --> 00:04:06.100
and I'm going to say LocalSystem here,

00:04:06.100 --> 00:04:08.540
and then I can enter that and close the quotes.

00:04:08.550 --> 00:04:12.240
So this is basically saying if it's local system‑‑‑ Well,

00:04:12.240 --> 00:04:15.950
if it's not the local system, then go ahead and find it.

00:04:15.950 --> 00:04:19.850
So I can do a check, and it will go and check all of the services that are there.

00:04:19.850 --> 00:04:25.310
And if the start name isn't LocalSystem, then it will not display those values.

00:04:25.990 --> 00:04:27.530
Now, that's all good and well,

00:04:27.530 --> 00:04:30.910
especially if you're doing an investigation into specific machines,

00:04:30.910 --> 00:04:32.290
you want to retrieve information,

00:04:32.290 --> 00:04:35.520
then we can use this kind of CIM instance command.

00:04:36.440 --> 00:04:42.410
Now the Invoke‑CimMethod invokes a method of a CIM class or a CIM instance.

00:04:42.730 --> 00:04:45.840
Once again, it uses the Windows Management Instrumentation,

00:04:46.500 --> 00:04:49.430
WMI, or the Component Object Model locally.

00:04:49.430 --> 00:04:54.730
And then it can use a CIM server if it's a remote or you're using a session.

00:04:55.070 --> 00:04:59.080
Now what's even more powerful is when you combine the

00:04:59.080 --> 00:05:02.090
idea of these CIM classes and instances,

00:05:02.090 --> 00:05:05.240
and then you use what's called Invoke‑CimMethod.

00:05:05.250 --> 00:05:10.390
So Invoke‑CimMethod allows me to execute something.

00:05:10.450 --> 00:05:13.360
Now remember, let's go back to my CIM class command,

00:05:13.360 --> 00:05:20.230
so I'm going to say Get‑CimClass, and I'll just use the Win32_Computer one again.

00:05:20.300 --> 00:05:21.530
So I'll press Enter.

00:05:21.670 --> 00:05:24.710
Remember we talked about the CIM class methods.

00:05:24.770 --> 00:05:33.940
So let me say system, and I'm going to say Select‑Object CimClassMethods.

00:05:33.940 --> 00:05:39.350
Now in the CimClassMethods for this specific one,

00:05:39.490 --> 00:05:42.230
you'll see that there are things that we can do.

00:05:42.240 --> 00:05:45.240
We can set the power state, we can rename,

00:05:45.250 --> 00:05:48.890
we can join it to a domain or workgroup or unjoin it.

00:05:49.180 --> 00:05:52.990
That's exactly the same when we start to look at other things too.

00:05:53.160 --> 00:05:54.820
So let's take this same command.

00:05:55.250 --> 00:05:58.100
I'm going to go back here, change it from Computer,

00:05:58.100 --> 00:06:00.790
and imagine that we're working with services.

00:06:00.790 --> 00:06:02.740
Now see what we can do.

00:06:02.750 --> 00:06:08.270
We can start, stop, pause, and resume services by using PowerShell.

00:06:08.270 --> 00:06:14.840
If I was to do one more here and change that from the Service to Process,

00:06:15.320 --> 00:06:17.020
so I'm looking for processes now,

00:06:17.130 --> 00:06:20.300
you'll see I can now create instances of processes,

00:06:20.460 --> 00:06:23.870
terminate them, and then get values from that as well.

00:06:24.350 --> 00:06:25.810
So how does that work?

00:06:25.820 --> 00:06:31.940
Well let's go ahead and try invoking the CIM method.

00:06:31.950 --> 00:06:33.410
So we call this first.

00:06:33.410 --> 00:06:36.720
We need to tell it the class that we like to use.

00:06:36.730 --> 00:06:41.800
So in this instance, I'm going to say the process one, so Win32_Process.

00:06:41.800 --> 00:06:45.560
And then, I get to say the name,

00:06:45.560 --> 00:06:49.830
and the name is actually the method that we want to execute.

00:06:49.830 --> 00:06:52.100
It's not just a random name of a process.

00:06:52.150 --> 00:06:55.760
So I'm going to say I would like to create a new process.

00:06:55.760 --> 00:06:57.890
Then we have what's called arguments,

00:06:57.890 --> 00:07:01.810
and the arguments is effectively this type of value.

00:07:01.810 --> 00:07:06.000
So it's just a set of values, like a key value pair.

00:07:06.010 --> 00:07:10.950
So I'm going to say CommandLine =, and then to be nice and simple,

00:07:10.960 --> 00:07:12.950
I'm going to say notepad.exe.

00:07:13.760 --> 00:07:14.550
So in reality,

00:07:14.550 --> 00:07:19.570
what this should do is create a new instance of the process called Notepad,

00:07:19.570 --> 00:07:20.890
so let's execute that.

00:07:21.010 --> 00:07:24.780
And sure enough, you can see right at the bottom, I have a new Notepad instance.

00:07:24.780 --> 00:07:25.840
And that's great.

00:07:25.850 --> 00:07:27.250
So think how we can do this.

00:07:27.250 --> 00:07:32.020
It means that we can now either locally or remotely invoke a CIM

00:07:32.020 --> 00:07:35.570
method and launch something on somewhere else.

00:07:36.210 --> 00:07:39.640
We also have the ability, if we were to think differently,

00:07:39.640 --> 00:07:42.260
we could also close specific things down.

00:07:42.430 --> 00:07:46.850
So let's say Invoke‑CimMethod like so.

00:07:46.850 --> 00:07:49.970
And this time, I'm not going to specify the class name.

00:07:49.980 --> 00:07:51.360
I'm just going to query.

00:07:51.370 --> 00:07:54.740
I'm going to say go and find me this specific process.

00:07:54.740 --> 00:07:56.600
And look how wonderful this is.

00:07:56.610 --> 00:08:01.500
We can say SELECT * FROM. Anybody recognize this type of syntax?

00:08:01.500 --> 00:08:04.870
It's almost like SQL code itself.

00:08:04.880 --> 00:08:11.390
So I'm going to say Win32_Process WHERE Name Like,

00:08:11.390 --> 00:08:17.460
and then I can say Notepad with an ampersand sign,

00:08:17.460 --> 00:08:20.130
which is exactly the same as you would do in SQL.

00:08:20.130 --> 00:08:21.510
And then,

00:08:21.510 --> 00:08:25.790
then I get to say, well, the method that I would like to use at this point,

00:08:25.790 --> 00:08:31.580
let me put that in quotes, sorry, is Terminate.

00:08:33.200 --> 00:08:34.640
There we go, Terminate.

00:08:34.650 --> 00:08:35.309
I'm going to press Enter.

00:08:35.309 --> 00:08:37.059
Now notice what happens.

00:08:37.059 --> 00:08:38.500
My Notepad disappeared,

00:08:38.500 --> 00:08:41.740
and it's been removed because it's queried the list of

00:08:41.740 --> 00:08:45.020
processes based on a SQL type statement,

00:08:45.020 --> 00:08:47.850
and then we said execute this specific method.

00:08:48.100 --> 00:08:52.390
So this is a great way using the CIM classes to be able to first

00:08:52.400 --> 00:08:54.690
get information that's stored on the machine,

00:08:54.820 --> 00:08:58.640
then get instances for the local machine or a remote machine,

00:08:58.640 --> 00:09:01.980
and then run through the same process of well maybe I want to retrieve

00:09:01.980 --> 00:09:06.650
information or maybe I want to invoke specific types of methods that are

00:09:06.650 --> 00:09:09.670
associated to those CIM instances and classes.
