This is the third part of the Availability Anywhere series. For your convenience you can find other parts in the table of contents in Part 1 – Connecting to SSH tunnel automatically in Windows
Today we will enable PowerShell Remoting in Windows. This sounds like a trivial task (you can google up an “easy” solution) but every time I’m doing that it happens that I need more and more commands because there are more and more settings to tweak. This note tries to summarize everything I captured over the last 10 years.
Server configuration — for receiving connections
First, run elevated PS and run this:
1 |
enable-psremoting -force -SkipNetworkProfileCheck -confirm |
Confirm everything and this in theory enables PS Remoting. It’s is not enough, though, because you need to allow multiple other things to get connections over IP (not using domain name) and without encryption (which we take care of using tunneling or VPN). Start with this:
1 2 |
netsh advfirewall firewall add rule name='PSRemoting HTTP' dir=in action=allow protocol=TCP localport=5985 netsh advfirewall firewall add rule name='PSRemoting HTTPS' dir=in action=allow protocol=TCP localport=5986 |
This configures your firewall. Now, you want to enable Windows Remote Management (WinRM). Open gpedit.msc
, browse to Computer Configuration > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM Service
and enable Open the Allow Remote Server management through WinRM
policy setting. Set filters to *
(unless you need something specific there).
Next, enable firewall rules for all profiles. In gpedit.msc
browse to Computer Configuration> Administrative Templates > Network > Network Connections > Windows Firewall > Domain Profile
and open Windows Defender Firewall: Define inbound port exceptions
policy setting. Enable it, click Show
button and add those port exceptions:
1 2 |
5985:TCP:*:enabled:WSMan 5986:TCP:*:enabled:WSMan |
Finally, you need to give permissions for particular users to connect to the machine. Run
1 |
Set-PSSessionConfiguration -ShowSecurityDescriptorUI -Name Microsoft.PowerShell |
and give permissions to users of your choice.
To verify your setting, run
1 |
winrm get winrm/config/listener?Address=*+Transport=HTTP |
and you should get something like
1 2 3 4 5 6 7 8 9 |
Listener [Source="GPO"] Address = * Transport = HTTP Port = 5985 Hostname Enabled = true URLPrefix = wsman CertificateThumbprint ListeningOn = 127.0.0.1, 169.254.148.232, 169.254.170.254, , ::1, fe80::24a9:613:3ca1:6de8%4 |
Client — for connecting to other machine over HTTP and IP address
You need to add trusted hosts using elevated PS:
1 |
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" |
Now you can connect using
1 |
etsn -computername IP_ADDRESS -port 5985 -credential DOMAIN\USER |
This makes an unencrypted connection using IP address. Obviously, you want to open this connection over VPN/SSH tunnel. If you want encryption, go with
1 |
etsn -computername IP_ADDRESS -usessl -port 5986 -credential DOMAIN\USER |