get-qaduser stop on error

When using 'get-qaduser samaccountname' if the samaccount name is not valid,  no error is thrown it just does not return anything

Is there a way to make it return an error if the samaccountname is not valid?

Top Replies

  • Hi  

    Get-QADUser is searching for a user object, it will either return nothing ($null) as no object matches your search, or it will return one or more user object(s) if one or more users were found.

  • Hi  

    Get-QADUser is searching for a user object, it will either return nothing ($null) as no object matches your search, or it will return one or more user object(s) if one or more users were found.

    If you wanting to throw an error, because the user doesn't exist, you can just "throw" an error in your script. IE

    $SamAccountName = TestU123

    $Result = Get-QADUser $SamAccountName

    If(-not $Result)

    {

         Throw "User doesn't exist"

    }

    or you should be able to use the below too

    If(-not $Result)

    {

         Write-Error -Message "User doesn't exist" -ErrorAction Stop

    }

    However, I would be useful to understand what your use case is, and how or where this check is being done? IE in an powershell script outside of ARS, in a Script Module called from an Administration Policy or Workflow etc.

  • yep, thats what Im doing just throwing an error if it returns nothing, was just curious if a way to stop by default if no value is found like get-aduser will, this is pulling some data outside of ARS where there are some invalid upns in the list I was given, appreciate it

  • I've wrangled with this in the past and I usually end up using Get-ADUser to perform an initial check instead (as long as I don't need any AR virtual attributes) as then I can use a try/catch plus Get-ADUser is faster - i.e.

    Try
    {

    Get-ADUser -identity $UserToCheckFor

    }
    Catch
    {

    Write-Host "Uh oh, user not found"

    }