So let's go back into our environment, and we'll first look at
how we create a PowerShell remote session.
We'll then look at how we reuse an existing remote
PowerShell session after we've disconnected.
And then lastly, we'll look at how we remove PowerShell sessions completely,
effectively closing them down.
So we're back on a Windows 10 workstation, and this
one is the one that's on the domain,
so there's no limitations of connecting here.
So what we want to do is actually create a remote session.
Now there's a couple of different ways of doing this, and our first
approach is to use what's called Invoke‑Command.
So Invoke‑Command allows us to pass in a ‑ComputerName.
So our ‑ComputerName, in this instance, will be 10.0.0.5,
which is the IP address of the Active Directory server, so
we'll utilize this as the connection.
Now,
of course, with this command we actually have what's
referred to as the script block.
Now the script block is the command or commands that we
wish to execute on that remote machine.
So what we're doing here is saying invoke a request to this
remote computer. And while you're there,
once you've got connected,
run Get‑ComputerInfo as a cmdlet. So if I press Enter here,
it's going to come back and say, okay,
connecting to the remote server failed. The WinRM client
cannot process the request, so there's some kind of
connectivity issues between the two.
Now we've already worked through some of these issues before of
how to communicate between various machines.
Now, of course,
we've also added trusted hosts to the various machines, so that's one
of the options that we're going to have to check.
So I'm going to paste in that same syntax and just choose
Get‑Item, and you'll see that that has no value.
So what we'll do is we'll go back and change that to
Set‑Item again on this machine, Set‑Item,
TrustedHosts, and we'll just do an asterix sign here, and we'll choose yes,
and then we'll clear that down, and then I'll go back and invoke my command,
so I'll click Enter. Now, same process again. You can see it's kind of a
little bit confused like what's going on, why is it not connecting? It failed
because of the authentication option, and, of course, maybe we need to pass
credentials to that machine.
So instead of just trying to invoke a request to that machine,
we obviously need to say, well actually, we're trying to connect in a
certain way. So, we already know how to do this, so let's create our
creds object here, so $creds = GetCredential, and we'll say ‑UserName
"TRAINING\Trainer" like so. We'll press Enter. It's asking me for a
message, so I'm just going to type the word Test, and then we'll put
the password in.
Okay, let's just make sure my creds object's working. Sure enough,
I've got one here. So, let's go back to Invoke‑Command, and I'm
going to go a little bit further up here to the IP address, and I'm
going to say ‑Credential $creds.
So we now have ‑‑‑‑ This is the computer I want to connect to,
this is the credential I want to use, and this is what I'd like you to execute.
So we're now going to press Enter, and you can see it's now
launching the Get‑ComputerInfo on that machine.
And you can see, even by just looking here where it says LogonServer,
it says the name.
If we scroll a bit further up here, you can see all of the details of
that machine. And as we scroll towards the top,
you'll see, you know, the network adapters,
the name, TRAININGAD.
So, it created a connection to that other machine,
and it was a one‑time connection.
It effectively went over and connected to it, and then we
turned the values that we had before.
Now, we have a different command that we can utilize here, so I'm
going to say Get‑PSSession and press Enter.
Now you notice we have no sessions because when we did the Invoke‑Command,
the Invoke‑Command is literally just invoking a command on
the other machine and returning a value. It's not meant to
be a reusable connection.
Now, what about if we wanted to use that approach?
Well, what we can do is if I just clear this here,
we'll come down a bit further,
I'm going to create a session. Call it Session1, a variable.
And what we'll do is we'll say New‑PSSession, which is another
way of connecting, and we'll pass in the computer name, which is
the same one as before, so 10.0.0.5.
And, of course, we have a credential object that we wish to utilize that just
makes sure that we connect, and I'm going to press Enter.
Now, if I look inside that session variable,
you can see that I have a remote machine connection
using PowerShell to that other machine.
So that's connected.
It has an ID of 4.
Now what I can do now is I can say, well, I would like to invoke
a command to that machine, but I already have a session, so I
don't need to pass a computer name.
So what I can do instead is we use the property called
‑Session, and then I can say use my $session1.
Once I've got my $session1, I can then use my script block
where I don't necessarily have to declare it,
but I can say Get‑ComputerInfo and press Enter. Notice what happens. It goes
into the existing PowerShell session and will then retrieve the same details
as before. But if I now say Get‑PSSession,
you'll see that my session still exists.
So it's a great way of getting a connection to a machine and then
being able to reuse it again and again. Because when we execute
commands, we can just drop them into that session ID. So let's
just clear that for a second.
Now, when we use these kind of,
I suppose, interactive sessions, then what we're able to do is effectively
run commands again and again into those environments.
Now, that session that we created is actually more a persistent session.
So it's not interactive. I'm not effectively running a
command, and it's running it on that machine.
Now, if we used Enter‑PSSession,
which is obviously one option that we've looked at in the past., so I'm going
to say 10.0.0.5, I'm going to pass my ‑Credential object just to make sure,
$creds, and press Enter here, you'll see that the interactive session
basically hijacks the current session that we've got, and it tells me that
I've got this IP address, and then it says this is where you can start typing
information that's there.
So, when we do an interactive session,
it's as if we are on that specific machine. So it works really,
really well.
It just means that I'm on this session on that machine,
and then when I'm finished with that, I'm then going to come back.
So, let me type exit here.
This will bring me back to there. I'm now going to go ahead and
create a New‑PSSession, a brand‑new one, ‑ComputerName, same as
before, like so, and we'll make sure that we have a credential
object of $creds. Press Enter.
Okay, so I've now got another one with an ID of 6. If I now
say Get‑PSSession, you should see both.
So I have two sessions. Now you may have thought that that would've said, no,
you already have a session, but that's not how that works.
I now have two remote PowerShell sessions to the same machine, which means
that I can drop in and out of those sessions as I need to.
So if I say Enter‑PSSession, I can say ‑Session, and I could say 6,
and you can go through. Of course, it's going to come back and
complain, but it's going to come back and say, well, what session
is it that you want to go into?
And I obviously need to pass a unique kind of session ID or have
retrieved the session to kind of enter that session again.
Now how does that work?
Well, from a syntax perspective, we're going to use Enter‑PSSession, and
then we're going to use the session variable. And then at some point, we
need to run Get‑PSSession, and you can see we've got some various options
here. So we can go and say get the name,
get the instance ID, if we need to.
So I'm going to say get number 6, and then we can press Enter.
Now, of course, notice what happens. If we do InstanceId, it gives
me a GUID, which isn't really the one that I'm looking for, but if
we say I and keep looping,
you'll see Id. I can now get that session back. So just be careful.
There's lots of IDs around, but you want the actual ID field. So I can
enter a session and then kind of drop into that session, but I can only
go into one session at any time. So I would need to exit. I can come
back. I can then get rid of 6 and type 4, and then I'm now in the 4
session. Even though they're the same IP address, it doesn't really make
a difference, but it lets me go in and out of those sessions. Now, of course,
that's great.
So if I just say Get‑PSSession, it listed both.
Now, if I wanted to clear down any of these,
then I can say Remove‑PSSession. And from the Remove‑PSSession option,
it's going to expect a session object or container.
You can see me scrolling through. It's got an Id option.
If I say 6, and say remove, and then go back to Get‑PSSession,
you'll notice that number 6 is now gone.
So we can manage creation of those remote sessions either by going
through the process of creating a standard one.
That's just an invoke command, which means it's a one‑time kind of connection.
Or we can drop that into a variable to make it a persistent connection, or
we can do what's referred to as the interactive connection, which means
that it's hijacking the current session we have open. It allows us to run
commands, and then it works from there.
Now, of course, we can also remove sessions fairly painlessly.
Now, of course,
we do have the option of modifying the way the session
connects, so we can use PowerShell options,
etc., and they get created, and then we can utilize them as needed.
So when we create connections out to other machines,
it's important to choose the right way of connecting, depending on how we wish to execute any of the commands.