Powershell remoting enables to work on a remote computer as you may be used on Linux using ssh. In difference to Linux, where this is usually straight-forward, I find it a bit more complicated on Windows - however achievable. There are two was a remote PS connection can be established - via HTTP or HTTPS.
Configure Remote Host
In order to get PS remoting working the host needs to be configured accordingly. First and foremost, ensure that there are no firewall rules blocking traffic on port 5985 and 5986 - no company firewall, no windows firewall. After that running “Enable-PSremoting” on the remote host should do the trick, so let’s try to connect from my working machine to remote host POWERSHELLTEST (10.10.10.123) which is btw. not in a domain - an information which is essential as you can see in a little:
Nope. So if the host name cannot be found, let’s try with the IP then….
Doesn’t work neither. Well at least the error message is more clear what I have to to if I want to use the IP address instead of the host name. However, more interestingly is that the host name cannot be found even so the name resolution is fine - I get a valid reply when I ping POWERSHELLTEST. So what is it then? After digging a bit I have found that the first error message is actually a bit misleading as you can read here:
When you are working with computers in workgroups or homegroups, you must either use HTTPS as the transport or add the remote machine to the TrustedHosts configuration settings. If you cannot connect to a remote host, verify that the service on the remote host is running and is accepting requests by running the following command on the remote host:
In any case with my current setup - remote host not in a domain - I have to connect via https. So let’s run winrm quickconfigas suggested:
My remote host seems already configured, so let’s check that
As you can see there are not TrustedHosts defined, so we need to set this. You may use a * which allows any host to connect, but this is not really recommended. So let’s only set my local computer (WINDOWS10) to the list:
Create a Session
Let’s give it another try to connect to the remote host:
For now I want to stick to the self-signed certificate therefore I have to find a way to disable the CA verification. Unfortunately New-PSSession does not list any specific parameter for that purpose, but -SessionOption {PSSessionOption} sounds promising. When looking into the details you can find the following
Specifies that when it connects over HTTPS, the client does not validate that the server certificate is signed by a trusted certification authority (CA).
Specifies that the certificate common name (CN) of the server does not have to match the host name of the server. This option is used only in remote operations that use the HTTPS protocol.
Good, that shall help, so let’s create a PSSessionOption object with these options …
… and try to connect again
Still not working. As usual, the error message does not give a real indication of the problem. If you check in description of New-Pssession you might realize that is uses current user for authentication per default. As the local user and the remote user are distinct ones, I have to specify valid credentials. So let’s create a credential object …
… and us this when creating your PS session.
Connect to Remote Host
Once the session options and the credentials are set properly the connection shall work
Alternatively you can also create the session with New-PSSession …
… and then connect to the session at any time