Search Active Directory via displayNamePrintable

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Search Active Directory via displayNamePrintable

Post by localpct »

What I'm looking for is a way to search Active Directory using our printable names as those are what our 3rd party ticketing system uses. I'm running into two issues

It doesn't export out to the array anything but the fullname which is the input I use so not surprising there
It takes several minutes per user - I'm assuming it's the logic of -filter and not -identity

Any help getting these two items to work?
  1. $aResults = @()
  2. $List = "John Doe"
  3.            
  4. ForEach($Item in $List){
  5.     $Item = $Item.Trim()
  6.     $User = Get-ADUser -Filter{displayNamePrintable -like "$Item" -and Enabled -eq $True} -Properties SamAccountName, Mail, telephoneNumber, POBox
  7.  
  8.     $hItemDetails = New-Object -TypeName psobject -Property @{    
  9.         FullName = $Item
  10.         UserName = $User.SamAccountName
  11.         Email = $User.mail
  12.         Tel = $User.telephoneNumber
  13.         MailCode = $user.POBox
  14.     }
  15.  
  16.     #Add data to array
  17.     $aResults += $hItemDetails
  18. }
  19.  
  20. $aResults
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search Active Directory via displayNamePrintable

Post by jvierra »

Get-AdUser -Filter "anr -eq 'j sm"\
Finds all names like "joe smith", "jim smith"... "Jay smalls" ...
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Search Active Directory via displayNamePrintable

Post by localpct »

J -

I have their printable names as they're exported from our ticketing software but I don't have their SamAccountName ( trust me I know, why wouldn't they also pull their username )

I want to find out AD properties for each of the given names

I know if I do like such

Get-ADUser usernameJohn -Properties * | select SamAccountName

I get just the SamAccountName of ther SamAccountName of usernameJohn

I need to do the same thing with the displayNamePrintable
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search Active Directory via displayNamePrintable

Post by jvierra »

There is no such property as "displayNamePrintable" in AD. Where are yu getting this property from?
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Search Active Directory via displayNamePrintable

Post by localpct »

1-6-2017 2-48-26 PM.jpg
1-6-2017 2-48-26 PM.jpg (19.93 KiB) Viewed 5177 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search Active Directory via displayNamePrintable

Post by jvierra »

So it is an Exchange property?

-FIlter "DisplayNamePrintable -eq 'John Doe'"
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Search Active Directory via displayNamePrintable

Post by dan.potter »

Declaring empty arrays and adding to them is old school. If you've created a custom attribute I highly suggest you prepend your company name or something to it.
  1. $List = "dan potter"
  2.  
  3. $results = ForEach($Item in $List){
  4.  
  5.     #$Item = $Item.Trim() There is nothing to trim here?
  6.  
  7.     $Users = get-aduser -Filter {anr -eq $item -and Enabled -eq $true} -Properties SamAccountName, Mail, telephoneNumber, POBox
  8.  
  9.  foreach($user in $users){
  10.  
  11.        [pscustomobject]@{    
  12.         FullName = $Item
  13.         UserName = $User.SamAccountName
  14.         Email = $User.mail
  15.         Tel = $User.telephoneNumber
  16.         MailCode = $user.POBox
  17.         }
  18.  
  19. }
  20.  
  21. }
  22.  
  23.  
  24.  
  25. $results
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Search Active Directory via displayNamePrintable

Post by localpct »

User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Search Active Directory via displayNamePrintable

Post by localpct »

Dan Solved it. We're all good

I was unaware of ANR

This takes all of two seconds now :D Thanks

Now to figure out how to get a form to autosize with a datagrid view
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked