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 mes开发者_C百科sage that user exist
}
else
{
#Create user
}
Am I missing something here ? Why do I get this error message ?
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.
精彩评论