WEBVTT

00:00:00.240 --> 00:00:02.850
So let's go straight onto our environment and we'll look

00:00:02.850 --> 00:00:05.250
at how we can use the Invoke‑Command.

00:00:05.250 --> 00:00:09.140
We'll execute this against a local window server first,

00:00:09.140 --> 00:00:12.540
and then we'll run this again from our client machine

00:00:12.550 --> 00:00:14.510
against a remote Windows Server.

00:00:15.260 --> 00:00:20.120
Now the Invoke‑Command, as we learned, allows us to execute commands,

00:00:20.120 --> 00:00:23.710
and we put them inside what's referred to a ScriptBlock,

00:00:23.720 --> 00:00:29.150
which is I suppose a set of squirrely brackets with commands that sit inside.

00:00:29.700 --> 00:00:34.800
Now we can execute this on single machines as in locally,

00:00:34.800 --> 00:00:38.230
or we can execute this against remote machines too.

00:00:38.290 --> 00:00:42.170
So the way this works, we type Invoke‑Command and then the ScriptBlock,

00:00:42.180 --> 00:00:44.410
which is just inside some squirrely brackets.

00:00:44.640 --> 00:00:47.690
As a simple example here, I'm going to say Get‑Service.

00:00:48.080 --> 00:00:51.440
So this is going to say run this command,

00:00:51.450 --> 00:00:53.230
whatever is inside the ScriptBlock,

00:00:53.230 --> 00:00:56.530
which in this instance is a regular PowerShell command locally.

00:00:56.650 --> 00:00:59.110
So I press Enter, and you can see the screen changes,

00:00:59.110 --> 00:01:03.770
and it lists me all of the services that are on the machine.

00:01:04.010 --> 00:01:05.290
Now, of course, we get everything back.

00:01:05.290 --> 00:01:07.770
We get stopped, started, running, etc.

00:01:08.150 --> 00:01:11.670
So let's clear this here, and we'll take that same syntax again.

00:01:11.830 --> 00:01:16.830
But this time, we can modify the ScriptBlock by combining things together.

00:01:16.840 --> 00:01:23.940
So I'm going to take the output of the Get‑Service, pipe it into a Where‑Object.

00:01:23.950 --> 00:01:24.870
And in the Where‑Object,

00:01:24.870 --> 00:01:30.850
we'll just do a very simple one where we say .Status equals and then,

00:01:30.850 --> 00:01:32.590
of course, I just want to look for Running.

00:01:33.410 --> 00:01:37.300
So what we're now doing is combining commands inside that ScriptBlock.

00:01:37.310 --> 00:01:38.370
So I press Enter.

00:01:38.370 --> 00:01:41.450
Now we only get the running ones that are local.

00:01:41.970 --> 00:01:44.240
Now of course, that's great being able to do it locally.

00:01:44.250 --> 00:01:44.750
But of course,

00:01:44.750 --> 00:01:48.530
the most important thing here is can we do this against remote machines?

00:01:48.530 --> 00:01:49.150
Now, of course,

00:01:49.150 --> 00:01:54.150
this does work with just Windows because we're trying to utilize services.

00:01:54.220 --> 00:01:58.490
So what we can do here is take the original syntax that we did.

00:01:58.490 --> 00:02:00.740
And to make it work on a different machine,

00:02:00.750 --> 00:02:01.900
I can say ComputerName,

00:02:01.900 --> 00:02:05.420
and then I'm just going to put an IP address here of one

00:02:05.420 --> 00:02:11.050
of the machines in my environment, which will be a Windows Server 2022.

00:02:11.060 --> 00:02:11.960
I'm going to press Enter,

00:02:11.960 --> 00:02:15.110
here and it's going to go ahead and return that

00:02:15.110 --> 00:02:17.090
information from that machine as well.

00:02:17.100 --> 00:02:20.670
So this isn't local anymore because we're using the ComputerName.

00:02:20.680 --> 00:02:26.630
Now if we just clear this here and then if I go back through my

00:02:26.630 --> 00:02:28.890
commands a little bit to the original one,

00:02:28.890 --> 00:02:31.570
you can see I've got my Where‑Object is Running.

00:02:31.570 --> 00:02:34.080
And of course, I can do the same thing here.

00:02:34.080 --> 00:02:38.420
So I can append the ComputerName object variable to it,

00:02:38.420 --> 00:02:44.790
so get the IP address again and run that same command remotely against

00:02:44.790 --> 00:02:47.630
the other machine to return only the running options.

00:02:48.250 --> 00:02:50.990
So what's great about the Invoke‑Command is that we

00:02:50.990 --> 00:02:53.070
can bundle up specific things,

00:02:53.480 --> 00:02:57.570
specific commands or certain types of filtering, and retrieve

00:02:57.570 --> 00:03:00.920
back those values, and it always comes back into the current

00:03:01.000 --> 00:03:02.780
contextual window that you're in.

00:03:02.950 --> 00:03:06.130
So, imagine that we wanted to take this command here where

00:03:06.130 --> 00:03:08.030
we just said go and get me the services.

00:03:08.040 --> 00:03:13.280
What about if I was just trying to retrieve processes instead? So I could

00:03:13.280 --> 00:03:16.370
say process, and then I can say go ahead and find them.

00:03:16.470 --> 00:03:19.110
Now of course, notice what happens. Nothing returns.

00:03:19.310 --> 00:03:24.070
And that's because I left the Where‑Object as status equals something.

00:03:24.290 --> 00:03:27.290
Now the status may not be a specific property, so let's

00:03:27.290 --> 00:03:29.700
remove that for a second and then press Enter.

00:03:30.090 --> 00:03:32.820
This will now go ahead and bring back that information.

00:03:32.820 --> 00:03:36.830
Sure enough, notice it doesn't give me a status. It just gives me

00:03:36.840 --> 00:03:40.840
a list, if I scroll here, a list of properties, which are just

00:03:40.840 --> 00:03:42.960
basic information around the processes.

00:03:42.980 --> 00:03:47.690
So when we combine things like Where‑Object or Select‑Objects or any other

00:03:47.690 --> 00:03:51.310
kind of query filtering, we need to make sure we have the right property

00:03:51.310 --> 00:03:53.730
values and the values that we're looking for.

00:03:53.740 --> 00:03:58.160
But Invoke‑Command is a great command to allow us to just say execute

00:03:58.160 --> 00:04:03.550
this in one go over here remotely or over here locally.
