0
votes

My local computer name is LOCAL-MACHINE, And remote computer name is REMOTE-MACHINE.

I expect issue powershell cmdlets on local computer, through Enter-PSSession connect to remote computer, and excute hostname.exe on pssession of remote computer, then exit.

I issue these command on local computer powershell ISE.

$password = ConvertTo-SecureString "Mypassword" -AsPlainText -Force

$cred=new-object System.Management.Automation.PSCredential("domain\user",$password)

Enter-PSSession -ComputerName REMOTE-MACHINE -Port 5986  -Credential  $cred -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck)

hostname.exe

Exit-PSSession

I expect GET resut:

REMOTE-MACHINE

But I get this result(local computer name):

 LOCAL-MACHINE

What can I do?

1
Do you actually see the console prompt changed to [REMOTE-MACHINE]: PS C:\.. ? if not you are not connected... also are you run it all at once? or line by line?Avshalom
Wait until Enter-PSSession has been executed, before executing hostname.exeMathias R. Jessen

1 Answers

0
votes

That is because Enter-PSSession does not work in a script. Use Invoke-Command instead:

$password = ConvertTo-SecureString "Mypassword" -AsPlainText -Force
$cred=new-object System.Management.Automation.PSCredential("domain\user",$password)

Invoke-Command -ComputerName REMOTE-MACHINE -Port 5986  -Credential  $cred -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -scriptblock {

    hostname.exe
}