PW script to pull create Initials from Firstname and Lastname in AD

Answered PW script to pull create Initials from Firstname and Lastname in AD

  • Thursday, February 21, 2013 10:36 PM
     
     

    I am trying to create a PW script to pull the users FIRSTNAME, LASTNAME, GIVENNAME and then I need to create the users Initials off of the Firstname/Lastname.   I was trying to use $AD_Intials = $firstName.substring(0,1)  but I keep getting an error

    ERROR: Method invocation failed because [System.DirectoryServices.ResultPropertyValueCollection] doesn't contain a method named 'substring'.

    The script works fine execpt when I try to use the substring command.   I am running the script against Windows 7 machines.

    Powershell script

    $user= $env:username
    $strFilter = "(&(objectCategory=User)(samaccountname=$user))"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = "LDAP://dc=TESTDOMAIN, dc=com"
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $ObjSearcher.PropertiesToLoad.Add("samaccountname") > $Null
    $ObjSearcher.PropertiesToLoad.Add("givenName") > $Null
    $ObjSearcher.PropertiesToLoad.Add("sn") > $Null
    $objSearcher.SearchScope = "Subtree"


    $colResults = $objSearcher.FindOne()
    $displayname = $colResults.GetDirectoryEntry()

    $firstName = $colResults.Properties.Item("givenName")
    $lastName = $colResults.Properties.Item("sn")
    $AD_Intials = $firstName.substring(0,1)

All Replies

  • Thursday, February 21, 2013 10:59 PM
     
      Has Code

    The error you get, tells you that you are trying to use substring on a PropertyValueCollection (Array).
    While the givenName property for obvious reasons won't contain more than one string, its still a PropertyValueCollection, and therefore you need to either enumerate it or select the index you want to use.

    $firstName | % { $_.substring(0,1) }
    $firstName[0].substring(0,1)


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

  • Thursday, February 21, 2013 11:09 PM
     
      Has Code

    If you type $firstName.Gettype(), you'll get

    IsPublic IsSerial Name                                     BaseType    

    -------- -------- ----                                     --------    

    True     False    ResultPropertyValueCollection            System.Col...


    which tells you it's not a String type object, therefore don't have a method called "Substring".

    Instead, if you go:

    $firstname = $colResults.Properties['givenname'].Item(0)
    $firstname.gettype()

    You'll get a String:

    IsPublic IsSerial Name                                     BaseType    

    -------- -------- ----                                     --------    

    True     True     String                                   System.Object

     


    For some reason you have to use all lowercase for the property name in this case, which I don't know exactly why.

  • Thursday, February 21, 2013 11:13 PM
     
      Has Code

    You could also just do this:

    $colResults.properties.givenname[0].substring(0,1)
    


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

  • Thursday, February 21, 2013 11:24 PM
     
      Has Code

    Oh Poppicock!

    Here is how to do this without allof the decoration.

    $user= $env:username
    $searcher=[adsisearcher]"samaccountname=$user"
    $user=$searcher.FindOne()
    $givenname=$user.Properties['givenname']
    $sn=$user.Properties['sn']
    $givenname[0][0]+$sn[0][0]

    Notice that none of the code is required.  We are getting one item by samaccountname which is unique across all of AD.  W eonly need to filter on teh samaccountname.

    We don't need to add properties as we are just grabbing one item.  Very fast.

    We then just use simple array notation to grab the first character of each item.  The searcher wraps everything in an array so the dereference is necessary. If you use GetDirectoryEntry() it is not needed.

    Not - 5 lines.

    Getting a correct understanding of how AD works is critical and will save you a lot of wasted time.  In this csae the issue is the Net classes and teh searcher and not AED that is an issues.

    Properties in AD are case sensitive and are all in lowercase.

    Once you have a clear understanding of how this is all wired together and why therie are variations you will stop listening to the urban myths about AD and learn to love the [adsisearcher] (Did Peter Sellars say that?)


    ¯\_(ツ)_/¯

  • Thursday, February 21, 2013 11:33 PM
     
     Answered Has Code

    Or as a one liner:

    ([adsisearcher]"samaccountname=$($env:username)").FindOne() | % { $_.Properties.givenname[0][0]+$_.Properties.sn[0][0] }
    


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog