So let's go back into the environment, and we'll look at how we execute
commands using both the script block and the script file.
We'll then look at how we execute commands for a single remote session,
including that script block again, as well as our custom PowerShell script.
And then we'll repeat the same process for multiple machines,
including those member servers in the Globomantics lab to be
able to retrieve the same information.
Now the most important thing about performing remote PowerShell is
obviously determining how you wish to execute the commands.
So are we going to execute a script block command or are we going
to execute a file command? And then, of course,
understanding whether we execute commands in single remote
sessions or across multiple remote sessions.
So first off, let's have a look at invoking some kind of connection.
So we use the Invoke‑Command that we've used before,
so Invoke‑Command. We'll pass it a ‑ComputerName,
which in this instance will be, let's just use our Active Directory
server again, and then we'll do the script block,
which we've used in the past.
But before we do script block, let me just add in the credential object here.
So ‑Credential and $creds.
But in our script block, we'll just use the standard one we've been using,
which is Get‑ComputerInfo, and we'll close that out.
So, this is going to be a single command against a single machine.
So it goes ahead and retrieves the various
information remotely against that machine.
Now, if I want to go ahead and change the machine,
let's say I want to run it against one of my member machines,
I'm going to say 10.0.0.10, it's going to repeat the same process,
but you can see I'm doing it one machine at a time.
And sure enough, if I scroll a bit further up here,
you should see the name of this machine,
which will be my training one.
So, clearly not my Active Directory server.
I could repeat that process three times.
So I can go back here and say I wish to execute that against
number 11, wait for that to return a value,
then go back and repeat on 12.
So, when we use the Invoke command,
we can connect to a machine on a single basis and just reexecute the same
commands again, and then we get those values returned.
Now, of course,
the downside to that is if you've got 50 machines you need to go onto,
it can be, obviously, a little bit more complicated.
So, let's look at how we can run the same thing,
but this time using various computers all stored together.
So let's go back and create our variable like we did before.
So you can see I've got 10.0.0.5, which is my AD server,
and then I'm going to list my member servers,
which is the ones we wish to execute the commands on, 11 and 10.0.0.12 like
so. So we have four machines that we wish to utilize.
And, of course, as before, if we do $computers, we get that list.
Now I'm going to go back to the Invoke‑Command syntax, and we're going to say
‑ComputerName. And then this time, we're just going to pass the computer
variable. I'm going to pass my credential object,
which is obviously what we need, so $creds, and then we'll do
as script block, we'll use the same syntax,
so Get‑ComputerInfo, and then close the script block, and I'll press Enter.
Now notice what happens. It's going ahead saying it's going to load it. It's
going to load it, and load it, and load it.
Okay, see that was really quick because, obviously it's just in a network,
but you can see that we have one of them listed here, which is let's
scroll here, that's my Active Directory server.
If we scroll a bit further up, you can see we've got another one.
So what it's done is it's retrieved all of the values and just executed
them one after that, so there's my member server 3.
If we go a bit further, they'll be 2, and then they'll be 1.
So it ran the command against every single one, but kind of very quickly.
So, let's make that a little bit easier to read.
So let's get rid of Get‑ComputerInfo, and we'll just say hostname, press Enter.
There we go.
And it just retrieves the hostname remotely on all of the machines. So to
execute what would be single commands on multiple machines, it's as simple
as passing in a computer object or a container that has a list of those machines, so fairly straightforward.