Collecting Active Directory data

Hi All !

I've been using a function to acquire the property of an AD attribute:

function lookupUPN{
    try{
        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.Filter = (&(objectCategory=User)(SAMAccountName=$Env:USERNAME))
        $objSearcher.SearchScope = Subtree
        $objSearcher.PropertiesToLoad.Add(userprincipalname) | Out-Null
        $resultsUPN = $objSearcher.FindAll()
        ac $logfile "UPN search completed: $($resultsUPN[0].Properties.userprincipalname)"
        return $resultsUPN[0].Properties.userprincipalname   
    }catch{
        ac $logfile "Failed"
  
    }
}

this works like a charm and gives me the users' UPN.

Now I would like to acquire the property of the attribute postalCode with this script: 

function lookupLocation{
    try{
        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.Filter = (&(objectCategory=Organization)(SAMAccountName=$Env:USERNAME))
        $objSearcher.SearchScope = Subtree
        $objSearcher.PropertiesToLoad.Add(postalcode) | Out-Null
        $resultsLocation = $objSearcher.FindAll()
        ac $logfile "Location search completed: $($resultsLocation[0].Properties.postalcode)"
        return $resultsLocation[0].Properties.postalcode  
    }catch{
        ac $logfile "Failed"
    }
}

Unfortunately, this code wont get me the data..
I keep getting 'Location search completed: ' with no location in it, while it is OK in AD..

I cant really figure out what i'm doing wrong here, i looked at the class in the AD schema..
Can someone see what the problem is ? :)

many thanks !

kind regards,

Thomas

August 31st, 2015 9:38am

function lookupLocation{ Try{ $searcher=[adsisearcher]"(SAMAccountName=$Env:USERNAME)" [void]$searcher.PropertiesToLoad.Add('postalcode') $account=$searcher.FindOne() $account.Properties['postalcode'][0] } Catch{$_} }

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 12:56pm

No such objectCategory as "Organization". You probably mean to use "user". But if you filter on sAMAccountName, you don't even need that clause, as sAMAccountName uniquely identifies the object in AD.
August 31st, 2015 1:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics