Why does New-PSSession keep prompting for credentials inside my script?

0
0
Asked By MellowPine42 On

An onboarding script that worked normally last week now gets stuck repeatedly showing the Get-Credential prompt when it tries to create a New-PSSession. The command works when run interactively outside the script, but the same looping behavior happens on two systems and for a coworker as well.

I tried forcing Kerberos authentication and also tested CredSSP, but neither changed the behavior. I initially suspected the do/while loop, although the script worked previously. No error code appears after the loop, and the log never records "Session not established," so the catch block does not seem to run.

The relevant code is:

$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)

What could cause the credential prompt to repeat without reaching either the success or catch-block messages?

3 Answers

Answered By AmberQuill19 On

Make sure you distinguish messages written to the console from messages written to the log while debugging. Write-Log is a custom function, while Write-Host should appear in the terminal. If neither message appears, execution is probably blocking inside Get-Credential before New-PSSession returns, rather than reaching the success or catch code.

MellowPine42 -

Write-Log is a separate function that writes to a text file. In this case neither Write-Host nor Write-Log runs after New-PSSession is reached, and Get-Credential keeps prompting.

Answered By CobaltLynx7 On

New-PSSession may be producing a non-terminating error, which would bypass catch. Try adding -ErrorAction Stop so failures are forced into the catch block. Also simplify the retry condition so the limit is easier to follow, for example checking whether $failcount -gt 4 and exiting at that point. It is also worth checking whether the previous successful run had a different $ErrorActionPreference value.

MellowPine42 -

I added -ErrorAction Stop, but the behavior was unchanged: there were still no visible errors or log entries, and the credential prompt continued repeating. I will still clean up the retry condition.

Answered By SilverMaple88 On

Step through the script in a debugger and inspect the values after each statement. In an editor such as Visual Studio Code, set a breakpoint on the New-PSSession line and then check whether the prompt is coming from Get-Credential, whether the session variable is ever assigned, and whether $sessionRestart changes afterward.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.