I am trying to come up with a simple function to verify credentials before running other commands in ExchangeOnline. Here is my problem(s): IF I run the function with correct credentials the first time, it proceeds as expected. But if the credentials are wrong the first time, it get stuck in the "Get-credential" loop even when I provide the correct credentials. can't seem to find what the issue is.
try
{
$Global:ErrorActionPreference ='Stop'
$Usercreds= Get-Credential -Message "Please enter your Elevated account credentials"
Connect-MsolService -Credential $Usercreds
Write-Host "`nSuccesfully authenticated" -ForegroundColor Green
Get-MsolDomain
}
catch
{
#$Global:ErrorMessage = $_.Exception.Message
$Global:ErrMsg= $Error[0]
Write-Host `n$Global:ErrMsg -ForegroundColor Red
}
}
DO{
verCreds
}WHILE($Global:ErrMsg -like "Authentication Error: *")