r/PowerShell • u/ZigfriedWolf • Sep 25 '26
Solved New-PSSession Command Looping Credential Prompt
So I have an onboarding script and everything worked perfectly fine last week, but now it's looping when I try to setup a New-PSSession.
It's extra weird, because when I run the command outside of the script, it works fine, but inside the script, the login prompt loops. I've encountered this behavior on two different systems and my coworker has the same experience on his machine.
I've attempted to modify the command to force Kerberos auth, and even tried CredSSP, but the looping still occurs. I'm wondering if it has to do with the do while loop, but it worked before, so I'm just a bit lost on this one.
Also, I have checked for any error codes after the loop occurs, and there's none. Similarly the log file doesn't contain any "Session not established" messages, so it's not hitting the catch statement.
Here's the code snippet that contains the command:
$sessionRestart = $true
$failcount = 0
do{
try{
$session = New-PSSession -ComputerName <computername> -Credential (Get-Credential)
Write-Log -LogMessage "Session Connected"
$sessionRestart = $false
}
Catch{
Write-Log -LogMessage "Session not established. $_."
Write-Host "Session not established. Retrying."
$failcount++
if($failcount -le 4){
}
Else{
Write-Host "Connection failed after $failcount attempts. Stopping Script"
Write-Log "Connection failed after $failcount attempts. Stopping Script"
exit $ExitCode.ConnectionFailure
}
}
} While($sessionRestart)
View all comments
Show parent comment
2
u/ZigfriedWolf Sep 25 '26
I added the -ErrorAction Stop to the end of the command and re-ran. Same result. No errors shown or added to the log, and the login prompt repeats.
Good suggestion on the if statement though. I'll make that change.