1
votes

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!

1
Do you have a more complete example? For example, how/when is $credential defined and what happens to it between runs of the script? - boxdog
$Credential is defined by a selfmade functions which gets the credentials from a file. But it is the same behavior when doing $Credential = Get-Credential. After that nothing happens with it (I can not set it to $null) and it's defined again in the next run. But it is always present, when im looking at the object after definition there is the username and a SecurePasswordString in it - MichalskiM
VSCode has been out of sorts for me for a long while and not real sure what its confusion is. I have had similar things happen even with just running code locally. Yet, I tested your use case in the console host, ISE, VSCode console and the VScode integrated console and could not repro what you are seeing / reporting. All things work. Not saying it is not happening to you, as noted I have my own weirdness going on for months now, on different client machines. Stuff just does not work or stops working and I have to restart. Very frustrating. So, maybe environmental on your end. - postanote
Thank you for your comment. I tested it in ISE too and its still happening. Even on another machine. Any tips where I can start looking for on the enviromental end? - MichalskiM

1 Answers

0
votes

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.