0

I am trying to RDP into a machine and run ipconfig using the following code:

 $pw = convertto-securestring -AsPlainText -Force -String abcdefg
 $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "AD\mon-abc",$pw
 $pc = '299-HR7BCH2'
 $session= Enter-PSSession -ComputerName $pc -Credential $cred
 $remoteIpconfig = Invoke-Command -Session $session -ScriptBlock { ipconfig }
 Remove-PSSession -Session $session

This is the error that I see in output:

Enter-PSSession : Connecting to remote server 299-HR7BCH2 failed with the following error message : The connection to the specified 
remote host was refused. Verify that the WS-Management service is running on the remote host and configured to listen for requests 
on the correct port and HTTP URL. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\Users\adhav\Desktop\rdp.ps1:4 char:12
+  $session= Enter-PSSession -ComputerName $pc -Credential $cred
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (299-HR7BCH2:String) [Enter-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed
 
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not 
null or empty, and then try the command again.
At C:\Users\adhav\Desktop\rdp.ps1:5 char:44
+  $remoteIpconfig = Invoke-Command -Session $session -ScriptBlock { ip ...
+                                            ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
 
Remove-PSSession : Cannot bind argument to parameter 'Session' because it is null.
At C:\Users\adhav\Desktop\rdp.ps1:6 char:28
+  Remove-PSSession -Session $session
+                            ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-PSSession], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemovePSSessionCommand

I think I am passing the PC name incorrectly and the session parameter is also generating error. Can someone please help?

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
Richaa Dhar
  • 51
  • 1
  • 2

2 Answers2

0

ok so I tried using Invoke:

$pc = "10.xx.xxx.xxx" $pw = convertto-securestring -AsPlainText -Force -String hfjkfffkff $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "AD\abcde",$pw Invoke-Command -ComputerName $pc -Credential $cred -ScriptBlock {Get-Process}

The OUTPUT IS:

Connecting to remote server 10.xx.xxx.xxx 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: (10.xx.xxx.xxx:String) [], PSRemotingTransportException + FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken

Richaa Dhar
  • 51
  • 1
  • 2
0

I was facing the same issue and below link helped me. Check this out.

I can't connect to remote server

Arch
  • 3
  • 1