We have to connect to untrusted machines via PowerShell. To do this, you have to add the machines,
First, if you have settings in there run this command to get a backup:
$trustedhosts = get-Item -Path WSMan:\localhost\Client\TrustedHosts
Then to get them to modify:
$trustedhosts.value
copy that out, add your hosts to it with comma delimited and add it with powershell:
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'Server2,Server2'
To use it in code (so it doesn’t ask you if you’re sure), you can force it by using the -Force switch
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'Server2,Server2' -Force
then check again:
get-Item -Path WSMan:\localhost\Client\TrustedHosts