Anarko_Bizounours
Anarko_Bizounours

Reputation: 501

Powershell Get-ADUser error message

I'm having trouble with my script. Everytime I try it, I get this error message :

Get-Adus: Can not find an object with identity "HAL.9000" under "DC=DOMAIN, DC= local".

I really don't know why I receive this error, because my script normally shouldn't show it.

Here is my script :

The function checking if user exist :

Function CheckUser
{
  param($NameUser)

  $check = get-ADUser -Identity $NameUser

  if($check)
  {
    $exist = 1
  }
  else
  {
    $exist = 0
  }
  return $exist
}

And there the code calling my function :

$exist = CheckUser $login
if($exist)
{
    #Prompt message that user exist
}
else
{
    #Create user
}

Am I missing something here ? Why do I get this error message ?

Upvotes: 1

Views: 4111

Answers (1)

JPBlanc
JPBlanc

Reputation: 72630

You just call the function CheckUser with "HAL.9000" as parameter that's why you've got this error ! If you want to avoid just protect with try/catch

Function CheckUser
{
  param($NameUser)

  try
  {
    $check = get-ADUser -Identity $NameUser

    if($check)
    {
      $exist = 1
    }
    else
    {
      $exist = 0
    }
  }
  catch
  {
   $exist = 0
  }
  return $exist
}

But you'd better debug your calling script to understand why this parameter is given.

Upvotes: 1

Related Questions