So let's go into our environment, and we'll look at how we
create a single computer remote session, and then we'll look at
how we create multiple computer remote sessions by connecting to
those extra member servers.
Now, creating sessions is really simple.
Now if we're connecting to a single computer, then of course, we do
have the ability to use what's called Enter‑PSSession.
I can pass my ‑ComputerName, and then I can say
10.0.0.5, which, of course, is my domain.
And then, of course, I've got a credential, and I can say I want to use the
$creds object. So I can press Enter here, and this will go and create me that
kind of interactive session because I'm entering into the session. Now if I
come out of that, I'm just going to say exit.
I also can use New‑PSSession, which is a little bit different.
The syntax is almost the same.
So let me do 10.0.0.5 here, and then we'll do ‑Credential,
and we'll say $creds, and press Enter.
Now notice the difference between the two. When you choose
Enter, what it does is it just creates a remote connection that
doesn't get listed as a session here,
a stored session, and it just lets you kind of take over
the console on that other machine.
So fairly straightforward.
So that's one way of doing it. But another way of doing it,
which I find personally for me is a little bit nicer is because I often
want to run a command multiple times or run multiple commands in the same
session, but not always at the same time.
So you can see that we've got this session that was created, and of course,
as you know, we can do Enter‑PSSession, and then I can say ‑Session. I could
say Get‑PSSession, and remember to use the ‑Id option 10, and press Enter.
So I'm now back into that session.
So, if it was me as a personal choice, I would want to use
the New‑PSSession so that I could always go in and out of
the sessions as I need them.
And, of course, if I just exit out of the session, I can also
then remove the session when I finish with it.
So it'll say ‑Session. I could say, well actually, we could just use
Id at this point, so I could say ‑Id and ‑Id of 10 and just remove,
which means that my PSSession objects, obviously, I've got one
existing, so let me get rid of that one too, so 4, and then
Get‑PSSession, I have nothing.
So, I've cleared all of my sessions down, so fairly
easy way of kind of managing.
Now things change when we start to connect to multiple machines at the
same time. Let's say we need to execute a specific command. Let's say
we stick with our Get‑ComputerInfo command that we wish to run against
something or we want to just make remote sessions to a whole bunch of
machines in one go.
So what we can do is the first approach we've got is to
create a simple array of IP addresses like so,
so there's my Active Directory server. I'm then going to say 10.0.0.10,
which is my member server 1, 10.0.0.11, and then 10.0.0.12.
So I've got four machines,
so one is Active Directory, and then three member servers.
So what does that look like when we kind of bring it back out as a variable?
There's my kind of structure of servers. Now, of course, I could have 100
servers in there. Now, when we want to create connections,
we can say New‑PSSession because we can't really enter the session because
it wouldn't return that back into the user interface.
So we can say ‑ComputerName, and this time around,
we can just pass in the variable.
I'm going to obviously pass my credential object to make sure that I
can connect, and I'm going to press Enter. Now notice what happened.
It generated my four sessions automatically and connected them here
in the list of my PowerShell session table and assigned them a
unique identifier.
So that means I can now go into each one, or I can invoke a
command into any one of those sessions.
So, this way we're able to create effectively persistent sessions
that we can then reutilize again and again.
So if I go back in and say Remove‑PSSession, and I'm
going to use the Id field and say 11, 12,
13, and 14, I should be able to then go to my sessions
table and have nothing, which is perfect.
Now, another option here is we can actually put them all into unique variables,
so we don't actually need to call the unique identifier. So if I do $srv01,
$srv02, $srv03, I can then say = New‑PSSession like so, and I can go back to my
‑ComputerName, and then I'm not going to pass the variable because the variable
won't really know what to do.
So I'm going to say "10.0.0.5", "10.0.0.10", "10.0.0.11", and
then "10.0.0.12", and then press Enter.
Now, notice what happened.
It kind of did a thing where it tried to kind of go through and connect
something, and it came back and said connecting to that failed.
It didn't quite know what to do with it, and then it tried
to connect to the next one, and it failed, and it had a bit
of an error that was going on.
Why did it do that?
Because we missed a parameter, which is clearly required, which is
‑Credential $creds, press Enter. Now, what did it do?
Well, it didn't error this time, which is fantastic.
So what do we now have?
Well, if I do Get‑PSSession, it's got four sessions that have been created.
If I go to $srv02, you can see that my session is session 20, and if I
say $srv03, that's going to be session 22 and 21.
Why has it dropped it into two?
Because I only created three variables, but I put four IP addresses.
So when obviously we define something like this,
we need to make sure it matches, or you're going to end up with
all of the sessions inside one single variable,
which actually might be the preferred approach when you're
trying to execute commands against them.
So the most important key here is that single connections to
servers or workstations can be done using either entering to give
you interactive or new to give a persistent.
But if we're using multiple machines,
it's often easier to create the variable or read a file that has the
server names in and then just create new PS sessions so that we can reuse them at any point in the future.