So, let's go back into our environment,
and we'll first connect to Windows Server using PowerShell
remoting, and then we'll connect to a Linux machine also
using PowerShell remoting.
So here we are on the Windows Client machine.
We also have a Windows Server machine.
Now, in order for us to connect remotely to the other machine,
we first need to do some prerequisites. And the first piece of
this is to actually run a PowerShell command,
which I'm just going to type here,
even though this isn't PowerShell, this is actually a command‑line
console, called Enable‑PSRemoting. So what we'll do here is we'll
do a quick search for PowerShell. I'm going to launch PowerShell 7
here. Let's maximize that.
It's important that we obviously use PowerShell, and I'm going to say
Enable‑PSRemoting, and then I can click the ‑Force option, and I'll just click
Enter here. Now, of course, notice it comes back and says access is denied, and
that's because you need to be running it as an administrator. So let's close
that and close that. We can then go Start, PowerShell 7, right‑click More, Run
as administrator. And we'll maximize that again, and then we'll say
Enable‑PSRemoting and ‑Force and Enter. Now, of course, this will go through
and do all of the configuration that's required, pieces to do with the WinRM
service,
which is the remote management service. Now, we need to do this on both
places, so we need to do this on the client machine, as well as the main
machine as well. Now, of course, before we go to the second machine, what
we also need to do is add in some information directly into what's called
the trusted hosts.
The trusted hosts is the IP addresses that are trusted from each side.
So if I do ipconfig here,
you can see that my IP address is 192.168.127.130, and
that's for the client, but then on my server, I'm going to
have a different IP address.
So let me just clear this here, and we'll just type in the command. It'll be
winrm set winrm/config/client, and then we'll say @{TrustedHosts=""}, and
then we'll do that. Now, in here will be the IP address of the machine, the
other machine we wish to connect to.
So I'm now going to flick over to the server. So we're now on the
Windows Server instead, and you can do this,
I can just say whoami. You can see it's win2022, or I could type in
hostname, just so you can see it's a different machine.
So let me just clear that here.
The first thing I need to do obviously here is do PSRemoting, exactly
the same as we did before. And I'll just ‑Force that. It's going to go
ahead and do the same thing as before.
Then what I'll need to do is get the IP address of this machine here so
I can utilize that in the other one, and also do the same, so winrm set
winrm/config/client @{TrustedHosts=""}. Now, of course, we know that it
was 192.168.127.130, and we can double‑check that. Now, while we're
here, let's open up Command Prompt, and we'll just get our IP address,
so 127.128. So let's flick back to the other machine and update that.
So we're back here, 127.128.
So that's going to be the IP address of the server.
So I'm going to press Enter, let's add it in, and then we can just
double‑check the IP address here, definitely 130.
So now let's flick back to the Windows Server. So we're back here.
Let's go back to our PowerShell window, and it's correct as it
should be, and we'll press Enter. So what we've now done is added a
connection between the two.
We've effectively said allow remoting and then trust each other.
Now the final step here before we can actually do anything is really we
need to restart a service, which in this instance is called the WinRM
service, which is the one for the remote management.
I'm going to flick back to my desktop and do the same thing.
Let me just clear it here so we can see it, and we'll say
Restart‑Service WinRM. And that will restart the service.
Now we have this set up, ready for a communication between the two,
what we can do is actually use a PowerShell command to test this.
So we can say Test‑WSMan, and then we can say what's the computer
that we're trying to get to?
So I can say 192.168.127.128 and press Enter. And then what we'll see is
it goes ahead and tries to make a connection, and you should just
basically see the information from the other machine.
So if there was an error here, it would fail.
So let's go across to the other machine and do the
same thing. We're back on the server.
So Test‑WSMan 192.168.127.130, and we get the same response come
back. So now we know that it's completely valid and the
configuration is working as expected. So, to initiate the remote
connection, let's go back to our desktop.
So here we are back on the desktop again. And the first
thing we can do is enter some credentials.
So I'm going to create a credential object using
Get‑Credential, which will ask me for the credentials.
Now, I know that my account over there is called Trainer, and then it's
going to ask me for the password, which I'm going to put in. Once I've
got that, I'm going to create another variable called session, and we'll
say New‑PSSession. We'll then populate the credential variable with the
values that we already populated in, so cred. And then, as we learned
previously, we can actually just put in the computer name, so
192.168.127.128, which is that machine. I can enter that. Now, do we have
a session?
Well, the first thing we have to do is we can check if the
session variable is valid, and sure enough, it is. We have a
WSMan Microsoft PowerShell connection.
If I say Enter‑PSSession and then take the session variable, it's
going to enter me into that specific machine, and you can see, well,
clearly we can see the difference, on this machine, I am the
trainer, and on my server, I am the administrator, and it has the IP
address that's there.
So now we could run specific commands in that line. So there you
go, I can do ipconfig, which is running remotely on the Windows
Server and returning the IP address.
I could also say whoami, and it will come back and say, well, you're a
Windows 2022 trainer, which is the actual account name, it just comes up
as administrator for its local directory structure. And I could then
obviously say, well, show me what's in the directory of the documents, or
I could navigate backwards and forwards through the structure and say
give me a directory.
And you'll see I've got Windows users, etc. Once I've
finished with my remote connection, I can simply say Exit‑PSSession,
like so. That will drop me back out. And then my session still exists, and so
we should learn to clean up our PowerShell sessions as well.
So I can say remove the PowerShell session session, and it's now removed.
So that's as simple as it is to connect from a
Windows Client to a Windows Server.