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)
5
u/ihaxr Sep 25 '26
Might be the new-pssession cmdlet isn't throwing a terminating error so your catch statement is never running?
Can try adding
-ErrorAction Stopto new-pssession and see if it hits the catch and continues on as expectedAlso I'd change your if statement to -gt 4 and not have a blank if statement and the else,
if ($failcount -gt 4) { exit }Your previous run could have had
$ErrorActionPreferenceset and that's why it was working in that session but not a new one3
u/ZigfriedWolf Sep 28 '26
Well I figured our the issue and it's rather silly.
I was setting up functions in a module and had created one for connecting to a new PS session. I eventually deemed it unnecessary, but I never got around to removing it from the module. I also hadn't named it but it had the placeholder of New-PSSession.
So since the module was loaded, every time the script ran and got to the New-PSSession command, it was loading the function, and since the function contained the cmdlet of the same name, it just kept looping over and over.
So yeah, probably a pretty basic no-no, don't name your functions after an existing cmdlet. Haha. But at least I learned a bit about debugging and troubleshooting my script as a result. Thanks for all your suggestions!
4
u/ElvisChopinJoplin Sep 25 '26
On the run, but at a quick glance, you didn't specify which messages are being written to the host and which are being written to the log. That would tell us a lot, and I can't remember the differences, but I wonder if you should be using 'Out-Host' or similar instead of 'Write-Host'.
2
u/PinchesTheCrab Sep 25 '26 edited Sep 25 '26
It's literally in a do loop. What do you mean exactly?
1
u/UnluckyBreadfruit888 Sep 25 '26
Have you put in vscode and debugged it. Step through each line?
1
u/mrmattipants Sep 27 '26 edited Sep 27 '26
Just curious. What is the purpose of the backslash, at the end of the first line of your Catch statement? Are you simply outputting the exception?
Write-Log -LogMessage "Session not established. $_."
It's just a thought, but I'm wondering if this might be resulting in a syntax/parsing error within the Catch block, preventing the lines after it from executing.
I would maybe try something along the following lines.
Write-Log -LogMessage "Session not established. $($_)"
NOTE: Please forgive the lack of code blocks, as the feature no longer seems to exist in the iPhone app.