The Surly Admin

Father, husband, IT Pro, cancer survivor

Employee Directory with Photo’s Part 2

Part two of my talk about converting my Employee Directory with Photo’s vbScript into Powershell.  Part one here.

ADSI to you too

Since using Get-ADObject isn’t going to work, that means we’re going to have to use LDAP and the ADSI Searcher to get this done.  Not my ideal way of doing it, but I know it will work.  Unfortunately it turned my one liner into this:

$Domain = New-Object System.DirectoryServices.DirectoryEntry($SearchBase)
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.SearchRoot = $Domain
$ADSearch.SearchScope = "Subtree"
$ADSearch.Filter = "(objectCategory=User)"
$PropertiesToLoad = "SamAccountName,distinguishedname,GivenName,sn,Title,department, TelephoneNumber,Mobile,facsimiletelephonenumber,mail,thumbnailphoto"
ForEach ($Property in $($PropertiesToLoad.Split(",")))
{ $ADSearch.PropertiesToLoad.Add($Property) | Out-Null
$Users = $ADSearch.FindAll()

So pretty much does the same thing as the Get-ADUser cmdlet, but it does include contact objects as well, which is a nice plus.  The other problem I ran into is that the LDAP field names are different from the Get-ADUser field names, and when using the ADSI Searcher you have to dot source the .Properties propety (did that make any sense?!)

What that means is when referencing a user, I can just use $User.title, I have to use $User.Properties.title.  The other problem is all of the property names are lowercase and case sensitive, so title works, but Title does not!  Beat my head up against that wall for awhile, let me tell ya!

On the plus side, one of the properties I load here is thumbnailphoto, and a simple modification of my earlier one-liner saved the photo data to a .JPG file!

If (-not (Test-Path $File))
{ If (($User.Properties.thumbnailphoto).Count)
{ $User.Properties.thumbnailphoto | Set-Content -Path $File  -Encoding Byte

One thing I wanted to avoid was saving a 0 byte .JPG file.  The Set-Content cmdlet will do that even if it doesn’t receive any information from the pipeline.  So by doing a simple check of .Count I can see if there is any data in the property, and count of 1 means there’s data and a $null means there’s nothing.  So above checks if the file already exists, if not check if the data is $null, if not save the .JPG file.


One thing this Employee Directory has that I’ve always liked is the filtering buttons.  The idea is for every unique department you could click on a button and see just that department.  I will admit this might break down for some people who have a TON of departments because HTML will pack those buttons in there pretty tight.  You might have to modify the department field to be entire divisions instead.  In vbScript figuring out all of the unique departments was a pain as you had to loop through all of the known departments and see if you got a match, if you didn’t then add that department to the known departments.  For every user.  Granted, vbScript is really fast doing this kind of thing but it’s messy.  Powershell has a much easier time of it.

$Buttons = $Data | Select Department -Unique | Sort Department

The Select cmdlet has a -Unique parameter which allows us to get all of the unique values that we specify.  Sort it for looks and then create a loop to put the buttons in place.

The Final Loop

We’ve built our Header, or table headings, our buttons and all of the photographs in AD have been extracted and saved as .JPG files.  Now all that’s left is to get put the employee’s in.  Setup a loop on our $Data field and simply go through every employee and add the proper HTML.  Once done combine all the different HTML variables and then output the full HTML to a file (on a web server in my case) and that’s it.  The whole project only took a couple of hours to convert from vbScript over to Powershell–the hardest part was getting the ADSI Searcher properties working properly.  Not bad at all!

Here’s a link to the source code on Spiceworks: click here


October 18, 2012 - Posted by | PowerShell | ,

1 Comment »

  1. […] keeping these posts at a manageable level, I’ll continue the discussion of the script later this week. Rate this:Share this:ShareLike this:LikeBe the first to like […]

    Pingback by Employee Directory With Photo’s Part 1 « The Surly Admin | October 18, 2012 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: