Powershell – How to connect to remote server using powershell

powershell

I am trying to connect to a remote server but getting the following error

[my ip] Connecting to remote server "my ip" failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OpenError: (my ip:String) [], PSRemotingTransportException
  • FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken

I am using this command to run a PowerShell script from my local machine on a remote PC

This is my script

$serverName = 'my ip'
$pwd = convertto-securestring "password12" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist   
".\Administrator",$pwd

Invoke-Command -computername $serverName {$("C:\_Projects\test.ps1")}

Edit:
my local pc and remote computer are not on the same domain . for example my local pc says mypc.test.local and remote computer says workgroup. Can some one help how to sort the above error by not changing the settings on remote computer, because its a UAT server.

Best Answer

You need to add -credential $cred to your Invoke-command command. You may also need to remove the .\ from the Administrator name--I don't have a workgroup server to test with, so I can't be sure if that will work.

Invoke-Command -credential $cred -computername $serverName {$("C:\_Projects\test.ps1")}

Related Topic