To Get-ADUser or not to GetADUser that is the question RRS feed

  • Question

  • I've been researching this for a month now and have read everything about variables in powershell and get-adobject I could get my greedy little hands on. I still don't get it.

    I tried to get an email address from AD, but I couldn't get the command to return the email address into a variable. It looks like the get-aduser command is not able to find the user because the $owners has the name stored as "John Smith." Can you please help me understand how to search AD for a user by a string containing first name space last name when everything in AD is stored last name comma first name so I can get this thing finished? Am I approaching this wrong? Would it be easier to convert "John Smith" to an email address using John.Smith at whatever? I've already tested everything else and verified that it's working. This is the only piece of a much larger script that is holding me up and worse making me feel like an idiot.

    $EmailAddress = Get-ADUser $owner -properties email -server ads.domain.local | select-object email

    I get this error :

    Get-ADUser : Error parsing query: 'Bill Jilesen' Error Message: 'syntax error' at position: '6'.
    At line:1 char:1
    + Get-ADUser -filter $owner -searchbase "dc=delaval,dc=local" -credential delaval\ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
        + FullyQualifiedErrorId : Error parsing query: 'Bill Jilesen' Error Message: 'syntax error' at position: '6'.,Microsoft.ActiveDirectory.Management

    Tuesday, June 17, 2014 6:16 PM


All replies

  • Hi,

    The -Identity parameter of Get-ADUser is expecting one of the following:

    DN, GUID, SID, sAMAccountName


    Putting a display name in won't return anything.

    EDIT: You can try using -Filter though:

    Get-ADUser -Filter "DisplayName -eq 'Bill Jilesen'"

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Tuesday, June 17, 2014 6:24 PM
  • Can I use -Filter with a variable? I have 84 some odd owners I'm iterating through. $owners is the variable that I'm temporarily storing the name of an owner in so that I can use it to build a csv then send an email to the owner with their specific CSV attached as well as text.
    Tuesday, June 17, 2014 6:49 PM
  • Yes, but you need to be aware that you might return more than one user with this query:

    Get-ADUser -Filter "DisplayName -eq '$owner'"

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by zwb0069 Wednesday, June 18, 2014 7:39 PM
    Tuesday, June 17, 2014 6:54 PM
  • I had to fiddle with the format of the owner variable because our display names are lastname, firstname in AD, but I did get it. Here's what I ended up with after your help.

    $ownerName = $owner #copies the server owners name to $ownername variable
    $Username = "$($Owner.Split(" ")[1]), $($Owner.Split(" ")[0])"
    $EmailAddress = Get-ADuser -Filter "Name -eq '$Username'" -properties emailaddress -server ads.domain.com | select-object emailaddress #gets email address for owner from AD

    This got me where I needed to be......of course then I had to get rid of extra text from the output, but I did that with trim. Thanks for the help!
    Wednesday, June 18, 2014 7:44 PM
  • Wednesday, June 18, 2014 7:51 PM