Search code examples
powershellvisual-studio-codepowershell-remoting

Powershell Implicit Remoting Session prompts for password when running the script twice


I have a problem regarding the implicit remoting feature in powershell, maybe someone can help.

Im opening a session with

$Session = New-PSSession -ComputerName $ComputerName -Credential $Credential -ErrorAction Stop

With $Credential I provide a valid credential object.

When running the script for the first time within a powershell window (or VS Code integrated console) everything works as expected and my commands are executed on the remote machine. When running the same script for the second time I can see that the session is opened but it still prompts for a password (username is already there). When I close VS Code, open it again run the script again it works like a charme. When I'm running the script within a temporary PS console it works.

I'm terminating the Session with

Remove-PSSession  $Session

Maybe someone has an idea.

Thanks in advance!


Solution

  • If anyone is facing the same problem. I found the solution.

    When "importing" the implicit remoting session it gets imported as a module. When "reconnecting" the session (either in the same script or in the same console window) and then executing a remote command it still uses the "old" module and therefore prompts for the credentials.

    So basically the solution is to remove the module with the "Remove-Module" command, in my case Exchange-Online.