An onboarding PowerShell script that previously worked is now repeatedly displaying the Get-Credential prompt when it tries to create a New-PSSession. The same behavior occurs on two systems, and a coworker has reproduced it as well. Running the New-PSSession command manually outside the script works normally. I have tried forcing Kerberos authentication and using CredSSP, but neither changed the behavior.
The script uses a do/while loop and a try/catch block:
$sessionRestart = $true
$failcount = 0
do {
try {
$session = New-PSSession -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)
No error code appears after the repeated prompts, the log does not contain the failure message, and the catch block does not seem to run. Could the session command be failing without throwing a terminating error, or could the loop be causing the prompt to repeat?
3 Answers
Debug the script one statement at a time and inspect `$sessionRestart`, `$failcount`, and `$session` after each attempt. A breakpoint before and after `New-PSSession` should confirm whether the command returns, throws, or remains blocked waiting for authentication. This can also reveal whether the loop condition is being reset unexpectedly.
Make sure you distinguish output written to the console from output written to the log while troubleshooting. `Write-Log` is a custom function, while `Write-Host` displays directly in the terminal. If neither message appears, execution may still be inside `Get-Credential` or waiting for the session command rather than reaching the success or catch blocks. Adding breakpoints or stepping through the script in an editor can show exactly which line is active.
`Write-Log` is a function elsewhere in the script that writes to a text file, and `Write-Host` should appear in the terminal. Neither runs after `Get-Credential` starts prompting, so the script seems to remain in that call or in the session creation.
New-PSSession may be producing a non-terminating error, which means the catch block will not necessarily execute. Try adding `-ErrorAction Stop` directly to the command so connection failures become terminating errors:
`$session = New-PSSession -ComputerName -Credential (Get-Credential) -ErrorAction Stop`
Also, the empty `if` block makes the retry logic harder to follow. It would be clearer to increment the counter and only exit once it exceeds the retry limit, for example with `if ($failcount -gt 4) { exit }`. It is also worth checking whether a previous session had a different `$ErrorActionPreference`, since that can change how failures are handled.
I tried adding `-ErrorAction Stop`, but the behavior was unchanged. No errors appeared in the console or log, and the credential dialog continued to reopen. I am going to simplify the retry condition as suggested.

I have opened the script in an editor, but I am still learning its debugging tools. Stepping through the command and inspecting those variables should help determine whether the loop is actually repeating or whether the credential prompt is being invoked again before the loop continues.