Powershell Remoting through Cloudflare Tunnel

I have a Windows server where cloudflared is running. I completed all steps to set up a tunnel in Zero Trust panel. WinRM service is running on the server, and its access and port are allowed in the firewall.
I also connected to the tunnel successfully from my client machine with:
cloudflared access tcp --hostname winrm.mydomain.com --url tcp://localhost:5985

Now I’m trying to start a remote Powershell session from my client machine using the command below:
Enter-PSSession -ComputerName localhost -Port 5985 -Credential (Get-Credential)

After providing my server credentials, Powershell returns this error:

Enter-PSSession: Connecting to remote server localhost failed with the following error message : MI_RESULT_FAILED For more information, see the about_Remote_Troubleshooting Help topic.

What could be the problem?

Aren’t you trying to connect to winrm.mydomain.com? Try using that instead of localhost.

It doesn’t work that way because what cloudflared does is to tunnel localhost’s connections on port 5985 towards winrm.mydomain.com.

After all, it is just a public domain for the tunnel. Had the target been accessible directly via this domain, that would violate the protection of tunneling in the first place.