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?