Pergunta

Im trying to create a while loop that request the correct user input before the script continues.

What I wan't is to make while loop that runs if the value is NOT true. So if the user doens't exist the while loop ask for a new username but i can't figure out how to do this. Se the example down below that works if the user exists. I have tried -ne $true and -not i different ways without success.

while(Get-ADUser -Identity USERNAME)
{
Read-host "Please enter a existing user"
}
Foi útil?

Solução

do
{
    $username = Read-host "Please enter an existing username"
} while ($username -eq "" -or (Get-ADUser -Filter {Name -eq $username}) -eq $null)

This is case insensitive though. This one might work better:

do
{
    $username = Read-host "Please enter an existing username"
} while ($username -eq "" -or (Get-ADUser -Filter "Name -like `"$username`"") -eq $null)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top