So let's go straight onto our environment and we'll look
at how we can use the Invoke‑Command.
We'll execute this against a local window server first,
and then we'll run this again from our client machine
against a remote Windows Server.
Now the Invoke‑Command, as we learned, allows us to execute commands,
and we put them inside what's referred to a ScriptBlock,
which is I suppose a set of squirrely brackets with commands that sit inside.
Now we can execute this on single machines as in locally,
or we can execute this against remote machines too.
So the way this works, we type Invoke‑Command and then the ScriptBlock,
which is just inside some squirrely brackets.
As a simple example here, I'm going to say Get‑Service.
So this is going to say run this command,
whatever is inside the ScriptBlock,
which in this instance is a regular PowerShell command locally.
So I press Enter, and you can see the screen changes,
and it lists me all of the services that are on the machine.
Now, of course, we get everything back.
We get stopped, started, running, etc.
So let's clear this here, and we'll take that same syntax again.
But this time, we can modify the ScriptBlock by combining things together.
So I'm going to take the output of the Get‑Service, pipe it into a Where‑Object.
And in the Where‑Object,
we'll just do a very simple one where we say .Status equals and then,
of course, I just want to look for Running.
So what we're now doing is combining commands inside that ScriptBlock.
So I press Enter.
Now we only get the running ones that are local.
Now of course, that's great being able to do it locally.
But of course,
the most important thing here is can we do this against remote machines?
Now, of course,
this does work with just Windows because we're trying to utilize services.
So what we can do here is take the original syntax that we did.
And to make it work on a different machine,
I can say ComputerName,
and then I'm just going to put an IP address here of one
of the machines in my environment, which will be a Windows Server 2022.
I'm going to press Enter,
here and it's going to go ahead and return that
information from that machine as well.
So this isn't local anymore because we're using the ComputerName.
Now if we just clear this here and then if I go back through my
commands a little bit to the original one,
you can see I've got my Where‑Object is Running.
And of course, I can do the same thing here.
So I can append the ComputerName object variable to it,
so get the IP address again and run that same command remotely against
the other machine to return only the running options.
So what's great about the Invoke‑Command is that we
can bundle up specific things,
specific commands or certain types of filtering, and retrieve
back those values, and it always comes back into the current
contextual window that you're in.
So, imagine that we wanted to take this command here where
we just said go and get me the services.
What about if I was just trying to retrieve processes instead? So I could
say process, and then I can say go ahead and find them.
Now of course, notice what happens. Nothing returns.
And that's because I left the Where‑Object as status equals something.
Now the status may not be a specific property, so let's
remove that for a second and then press Enter.
This will now go ahead and bring back that information.
Sure enough, notice it doesn't give me a status. It just gives me
a list, if I scroll here, a list of properties, which are just
basic information around the processes.
So when we combine things like Where‑Object or Select‑Objects or any other
kind of query filtering, we need to make sure we have the right property
values and the values that we're looking for.
But Invoke‑Command is a great command to allow us to just say execute
this in one go over here remotely or over here locally.