Howdy Folks -
I’m trying to implement a loop that catches invalid credentials when connecting to the MSOLservice. My current code continuously re-prompts the user for the credentials (via Get-Credentials) when invalid credentials are detected, however, i’m finding that i am continuously prompted even after using the correct login information- it appears that the while loop never exits. I’m able to accomplish what i need through an If statement but this sets a limitation on the number of logon attempts permitted. I’ve confirmed the service connects with the valid logon credentials as i am able to run O365 cmdlets if I manually stop the script’s progress.
$UserCred = (Get-Credential) $Connection = Try{Connect-MsolService -Credential $UserCred -ErrorAction Stop} Catch{write-Warning "$($PSItem.Exception.message)"} while(!($Connection)) { $UserCred = Get-Credential $Connection = Try{Connect-MsolService -Credential $UserCred -ErrorAction Stop} Catch {Write-Warning "$($PSItem.Exception.message)"} }