From the course: Learning PowerShell Core

PowerShell remoting on Windows - PowerShell Tutorial

From the course: Learning PowerShell Core

Start my 1-month free trial

PowerShell remoting on Windows

- [Lecturer] PowerShell has a concept called sessions. Sessions are a way that PowerShell allows you to keep track of a connection to a remote computer. A session is what all remote commands are run under. The general workflow of running commands with PowerShell remoting is to first establish a session, which includes authenticating to the remote computer and then running some code inside of that session. So think of it as kind of like Telnet. You're able to run commands interactively in a remote session just like you're sitting in front of the computer. Or when you're writing PowerShell scripts, you can seamlessly pass code to the remote computer and have it run for you completely unattended. Sessions, or what I may be referring them to as PS Sessions, are a big concept we'll be covering in the demo for this lesson. So I'm now on my what soon will be a remote computer. So this is my Windows server, and as you can see here, I'm not running PowerShell core. I'm running Windows PowerShell version 5.1. This is on my Windows server. I'm not on my typical Windows 10 machine that I've been working with in the past. I need to get this set up so that PowerShell remoting will work so I can establish this session to this machine. Because by default, any Windows server after 2012 R2 will have PowerShell remoting enabled, but you may not have the service running, there's a couple firewall exceptions you have to do, and that sort of thing. So to ensure that PowerShell remoting will work for this, we can run Enable PSRemoting. Now what this does is this just goes out and checks if the WinRM which is the protocols that PowerShell remoting uses is enabled, there's a couple firewall exceptions that it does and generally it just makes sure that the Windows server is configured properly. You can also run WinRM quickconfig. That used to be the go to. They generally do the same thing. However, you can see here that WinRM quickconfig configures a local account token filter policy. So, I'm not going to go to that in depth by any means. For now, let's just say, yes, I want to make the change. And then it says WinRM has been updated for remote management, blah blah blah, you're good to go. That's what's all required to enable PowerShell remoting. Now I am taking into consideration is I don't have any problem with the firewall blocking ports, that sort of thing, so you will need to make sure that your firewall allows the WinRM or PowerShell remoting communication. All right, now we're back on our Windows 10 computer, our local computer, and I'm back in PowerShell core. The next thing I have to do is make sure that this machine is set up to be a WinRM or PowerShell remoting client. I can do the same thing on this machine by running Enable PowerShell remoting. And now you can see that it did update some information that we needed for the WinRM service but it did give us an error message. It says a message about the WinRM firewall exception. This is something that gets a little bit annoying sometimes because this machine has a firewall profile of public set up on it. So what we can do is we can skip that by using the skip network profile check. You run this, it will go through all the configuration that it needs and then now it should be set up to be a WinRM client and actually a server if we needed to do that. All right, next up, I'm going to hit the up arrow here and go through some steps with you. First thing we have to do is we need to specify a credential, we have to provide a credential which is a username and password to connect to that remote computer because we are not in an Active Directory environment. If you were in an Active Directory environment, it would use Kerberos and you wouldn't need to explicitly provide the credential. So I'm going to provide my username of Adam and my password. And I'll come up here a little bit in the history. And then we need to run Invoke Command. So Invoke Command is the command that you run against the remote computer. This is the command that sets up a temporary PowerShell remoting session on the remote computer. So the computer name is 10.0.0.6, that's the IP address of a remote Windows server. We're passing the credential with a credential parameter, and then inside of the ScriptBlock there you'll have host name. Inside anything between the curly braces, that's any kind of code that you want to execute on the remote machine. In this case, we're just executing host name, just to see if it comes back as the name of that Window server. All right, if I hit Enter, now you notice that it comes back with an error. There's a couple different configuration things that have to happen here to get this set up. It's saying something about trusted host. If you're not in an Active Directory environment, PowerShell remoting needs to trust that remote machine. So to do that, what I can do is I can run set item wsman:\localhost\Client\TrustedHosts, have a value of star, that trusts everything. If you wanted to be more security conscious, you can just put in the host name of 10.0.0.6, but for now, I'm just going to let it trust everything. Confirm the warning, and then now let's try it again. All right, now you can see that WINSERV19, that is the name of the remote host. So we have successfully set up PowerShell remoting across Windows 10 and the Windows Server 2019 host.

Contents