Powershell – Unable to use PowerShell Enter-PSSession to connect to remote server

powershellremote-management

I am having problems connecting to a remote server using PowerShell where the remote machine uses a non-default port number. The setup is as follows: I have a virtual host server with several virtual machines. All of these virtual machines have the same IP address but are accessed with a different port, for example:

a.b.c.d:3000
a.b.c.d:3001
etc

So, the PowerShell script I have so far is:

$password = ConvertTo-SecureString "<MyPassword>" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("<Domain\UserName>", $password)
Enter-PSSession -ComputerName <IPAddress> -Port <PortNumber> -Credential $cred

The bits inside the "<>" are specific to the individual machines. When running this script I get the following error:

Enter-PSSession : Connecting to remote server failed
with the following error message : The client cannot connect to the
destination specified in the request. Verify that the service on the
destination is running and is accepting requests. Consult the logs and
documentation for the WS- Management service running on the
destination, most commonly IIS or WinRM. If the destination is the
WinRM service, run the following command o n the destination to
analyze and configure the WinRM service: "winrm quickconfig". For more
information, see the about_Remote_Troubleshooting H elp topic. At
C:\PowerShell\Test7.ps1:25 char:16
+ Enter-PSSession <<<< -ComputerName -Port -Credential $cred
+ CategoryInfo : InvalidArgument: (:String) [Enter-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed

Another variant I tried is as follows:

$password = ConvertTo-SecureString "<MyPassword>" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("<Domain\UserName>", $password)
$powershell_uri = "http://<IPAddress>:<PortNumber>"
Enter-PSSession -ConnectionUri $powershell_uri -Credential $cred

but this gave the following error:

Enter-PSSession : Connecting to remote server failed with the
following error message : The client cannot connect to the destination
specified in the request. Verify that the service on the destination
is running and is accepting requests. Consult the logs and
documentation for the WS- Management service running on the
destination, most commonly IIS or WinRM. If the destination is the
WinRM service, run the following command o n the destination to
analyze and configure the WinRM service: "winrm quickconfig". For more
information, see the about_Remote_Troubleshooting H elp topic. At
C:\PowerShell\Test7.ps1:21 char:16
+ Enter-PSSession <<<< -ConnectionUri $powershell_uri -Credential $cred # -ComputerName -Port -Credential
$cred
+ CategoryInfo : InvalidArgument: (http://:/:Uri) [Enter-PSSession],
PSRemotingTransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed

I have set the TrustedHosts on my local machine (winrm set winrm/config/client @{TrustedHosts=""}) and on the remote machine I have run the "winrm quickconfig" command. On the remote machine I have also run the "winrm create winrm/config/listener?Address=*+Transport=HTTP @{Port=""}" command.

Any assistance on how I can establish a connection within PowerShell to these machines would be greatly appreciated.

Best Answer

On the remote computer:

  1. In: Control Panel\Network and Internet\Network and Sharing Center
    Make sure the remote computer is not in the public location, but set it to work or private

  2. Start PowerShell in administrator mode and enter the command:
    Enable-PSRemoting
    exit

  3. Goto Control Panel -> System and Security ->Windows Firewall and click advanced Settings
    Add the ip-range of your managing computer to windows remote management(http-In) both in the private and in the domain inbound rules.

On the managing computer:

  1. Start PowerShell in administrator mode and enter the command:

    Set-Item WSMan:\localhost\Client\TrustedHosts -Concatenate remotecomputer.domain.suffix -Force

    using your complete remote computer's network path. This adds the remote computer network name to your trusted hosts.

That should do the trick.

Related Topic