The Surly Admin

Father, husband, IT Pro, cancer survivor

Employee Directory With Photo’s Part 1

One of my favorite projects this last year was creating an Employee Directory with Photo’s webpage.  Of course, with me it’s never enough just to create a static page, it needs to be coming straight from a central database like Active Directory.  This was a tough project because it also involved using Javascript on the page itself to do some on page searches.  I know very little Javascript and found it very challenging to figure this part out, and even had to reach out to the Spiceworks community for some help (thanks Snozzberries) on some of the coding.  Written in vbScript it was 383 lines of goodness.

But I’m all about Powershell now, and it was time for a re-write!

Background

My original script was very much written for the environment I worked in.  At SeraCare we have 3 locations: Milford, Gaithersburg and Frederick and it was easiest to segment users by those locations.  But I didn’t want to go through and edit every user and set their location field to those values (lazy).  So I setup a capability in the script to set default values for the location based on the OU where the Active Directory object was located.  This involved using 2 strings, the first a string search for the OU (and it had to be unique enough that you didn’t get false positive hits) and then a second string with the default name.  So “OU=Gaithersburg” would be in the first string, and “Gaithersburg” would be in the second strong, etc.  These were comma seperated values that were split into arrays later on by the script.

And this system worked quite well, but difficult to implement, I think.  That means with the Powershell version I decided to simplify.  It’s my assumption that most people are more prone to use the Department field then the Location field so I changed the script to use that.  I am still keeping the ability to selectively exclude people by simply putting the word “Exclude” in the department field though.

I won’t go into the usual detail of going through the scripts like I normally do just because it’s still a 256 line script, but I’d like to highlight some things I think are cool, or are new to me.

Here-String

The here string is a cool feature of Powershell that allows you to define a very large string.  The cool part is it allows characters you normally have to “escape” to get, such as double quotes and others.  This is great because as part of this script we have to define large sections of HTML code that’s static and the here string makes it easy and very readable.  To define a here-string, you declare your variable and have it equal @” then you put all of your text in and the last line (by itself) is “@.  For example:

$myString = @"
The little nut brown hare said, "How much
do I love you?"
"@

$myString will now output exactly:

The little nut brown hare said, “How much
do I love you?”

This made coding the HTML so much easier within the script!

To use RSAT or not?

This was the next big question, and originally the answer was yes I will use it.  Why?  Getting all of the user objects to be processed was a beautiful one liner.  It’s hard to argue with something so elegant as this:

$Users = Get-ADUser -Filter * -SearchBase $SearchBase -SearchScope Subtree -Properties Title,Department,TelephoneNumber,MobilePhone,Fax,EmailAddress

And it worked wonderfully.  In addition, there’s a beautiful little cmdlet for pulling the photo’s out of AD too I could use:

ForEach ($User in $Users)
{  $User.thumbnailPhoto | Set-Content -Path $OutputPath  -Encoding Byte }

And this worked perfectly too.  Then I ran into a major snag.  One thing I wanted to replicate in the new script that the old script could do was also including Contact objects.  No problem, I thought, I can just use the Get-ADObject cmdlet to get those.  But Get-ADObject was missing to critical pieces of information MobileNumber and telephoneNumber.  The contact object contains those fields, but Get-ADObject simply doesn’t use them.  Now, there’s a good chance that if it’s a contact object and not a user object they won’t be assigned a telephone number, and you could probably live without MobileNumber but I could think of plenty of scenario’s where that information would be needed.  And besides, I didn’t want to compromise the script by taking the easy way out.

In the interest of keeping these posts at a manageable level, I’ll continue the discussion of the script later this week.

Advertisements

October 15, 2012 - Posted by | PowerShell | , , ,

6 Comments »

  1. Superb! I am using this currently and have already imported this into our company Intranet as a webpart. I hope you don’t mind but I added a small java script to enable a searchbox which will allow our users to search for users based on any information on the presented HTML. Problem is, like you, I am VERY limited in Java scripting and that damn searchbox sits on the very right of the page whereas I want it on the left, so I’m gonna go out on a limb here and ask if you know how to do that???

    Here is the script I’m using:


    var NS4 = (document.layers);
    var IE4 = (document.all);
    var win = window;
    var n = 0;
    function findInPage(str) {
    var txt, i, found;
    if (str == “”)
    return false;
    if (NS4) {
    if (!win.find(str))
    while(win.find(str, false, true))
    n++;
    else
    n++;
    if (n == 0)
    alert(“Not found.”);
    }
    if (IE4) {
    txt = win.document.body.createTextRange();
    for (i = 0; i 0) {
    n = 0;
    findInPage(str);
    }
    else
    alert(“Sorry, we couldn’t find.Try again”);
    }
    }
    return false;
    }

    Comment by Richard Hipkin | October 16, 2012 | Reply

    • Um, ignore coding – here I thought I had cracked Java but there was already a search bar 😦

      Comment by Richard Hipkin | October 16, 2012 | Reply

      • No worries! We’ll chalk it down as excitement 😉

        Comment by Martin9700 | October 16, 2012

  2. […] day Richard Hipkin over at Spiceworks requested a custom version of the Employee Directory that I blogged about last week.  The irony, and isn’t this always the case, is that he wanted something I […]

    Pingback by Custom Employee Directory Update « The Surly Admin | October 25, 2012 | Reply

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

    Pingback by Employee Directory with Photo’s Part 2 « The Surly Admin | October 29, 2012 | Reply

  4. […] getting requests “Can I get this field?” or “Can I get that field?” for the Employee Directory script and honestly it wasn’t designed for that.  The header in the table has to be manually […]

    Pingback by New Version: Employee Directory « The Surly Admin | May 16, 2013 | Reply


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: