简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM