So let's first focus on creating single and multiple remote sessions.
So firstly we have the two different types here. So you can execute a
single connection to a remote machine and it could be done using
Enter‑PSSession or maybe New‑PSSession if you want it to, and then we have
the idea of connecting to multiple machines at the same time, invoking
multiple sessions, and that can be done using the Invoke‑Command where you
provide the list of remote machines as kind of a parameter to say I want
to execute this.
So think about Globomantics kind of architecture here. So we have our
workstation with our multiple machines, and of course we're going to
expand our lab to add more servers, but effectively I can say go and
connect to the Active Directory server,
the file server, and all the other servers, and run this
single command against all of them.
From a single remote session perspective, we can enter the
PowerShell session and we can create New‑PSSession, and that
will start an interactive session.
So commands get typed in your machine, so if I'm on the admin workstation,
I'm going to type the syntax or the commands, whatever I need to
do, on my machine, and it will be as if you are typing them on
that other machine. You are limited to one interactive session
at a time, so you can't, if it's a single remote session,
you can't be kind of dropping in and out all the time.
Either way,
if you come out of one you have to connect to the other, but you only
have one at a time. You would use the ComputerName variable as part of
the way of connecting to a single machine.
So you simply are saying Invoke‑Command or Enter‑PSSession or
New‑PSSession ‑ComputerName, and then specifying that single
computer name. Now to create multiple remote sessions, we're going
to utilize New‑PSSession and Invoke‑Command that will let you
create those multiple sessions.
We populate the ‑ComputerName variable with
effectively an array of computer names.
The commands are the same,
they're typed in the local computer, and it's as if they
were typed on the other machine. And all commands execute
on each of the remote computers.
So if you have 10 computers you're connected to,
then it will execute the command on all of those 10 machines one by one.
Now, in order for us to achieve this,
we've actually expanded the Globomantics lab.
So let's look at how this kind of breaks down.
So we have our Active Directory server,
we have our file server, and then we've introduced some member servers
into the equation, and you can see here I have Member Server 01, 02,
and 03. Then I have my Admin Workstation.
So in order for us to look at how we connect remotely and to execute commands,
we've increased the number of servers.
So the idea is here that me, as the administrator,
I wish to execute a command across Active Directory,
file server, and three more member servers,
which is kind of normal when you're looking after multiple
servers or a server farm within any organization.
So let's look at how we create a single remote session.
So it's really simple from a PowerShell perspective. We can
either use Enter‑PSSession or New‑PSSession, and we simply pass
the ComputerName, and the IP address used here is obviously the
Active Directory server.
Either of those will initiate a single one‑time session to be
able to connect backwards and forwards.
If I wanted to create multiple sessions, then let's say I created a
variable here with the IP addresses or the names,
you'll recognize those IP addresses, those are Member Server 01, 02, and 03.
I can then say New‑PSSession,
I can use ComputerName, and then you'll see I'm just going to pass the
variable. Now notice I don't have to do a loop statement,
I don't have to iterate through the IP addresses,
it understands it and will connect to all three and create sessions.
If I wanted to be really,
really clever and create, effectively, persistent sessions,
I could create PowerShell sessions for every one of those IP
addresses and put them into their own variable.
Now, what would be the advantage of that?
Well,
if we think back to when we talked about connecting back into
sessions or creating persistent sessions,
it's based around the session associated to a
variable that we can call back into.
So this would mean I would end up with $srv01, 02, and 03, each
containing a PowerShell session that relates to the IP addresses
that were passed as part of the ComputerName, allowing us to drop in and out much easily.