Naeso
Naeso

Reputation: 35

What's wrong with my do while statement in this PowerShell script?

do {
  $user = $null
  Write-Host "Please enter your credentials in order to connect you to the Azure AD."
  $user = Get-Credential

  if ($user -eq $null) {
    Write-Host "Bad credentials. Please retry."
  }
} while (!($user -eq $null))

This part of the script will allow the user to enter his credentials. I wanted to check if the user inputs something.

Problem is, if the user does not input something, the script works well and the user is forced to enter credentials.

But when I input something, I don't have any errors, but the script loops! It doesn't want to exit the loop.

Where am I wrong?

I've also tried

while ($user -eq $true)

but it doesn't work either. The user, even if he doesn't enter something, passed the while.

Upvotes: 0

Views: 1826

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Your loop continues as long as the user provides credentials ($user is not $null). You want to loop as long as the user does not provide credentials ($user is $null).

do {
    ...
} while ($user -eq $null)

Upvotes: 2

Related Questions