In my case the script failed because $remote_host was an ipv6 address. With the -replace it works even for those.