Archive for the ‘Programming’ Category

In my Exchange 2010 environment, we’re using the thumbnailPhoto attribute in active directory, so that users photos appear in Outlook 2010. We also have a custom intranet page that features a company directory which pulls details such as phone numbers from AD. I wanted to also add in the image stored in thumbnailPhoto for each user, so started to create a way of extracting the image that could be used in a web page.

I ended up with a C# ASP page, called userPhoto.aspx with the following contents:

<%@ Page Language="C#" %>
<%@ OutputCache Duration="6000" VaryByParam="u" %>
<%@ Import Namespace="System.DirectoryServices" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
        String myUser = Request.QueryString["u"];

        if (myUser == null)
            Response.Redirect("app_graphics/user.jpg");

        Response.ContentType = "image/jpeg";
        Response.Clear();
        Response.BufferOutput = true;

        DirectoryEntry de = new DirectoryEntry();
        de.Path = "LDAP://OU=Users,DC=domain,DC=local";

        DirectorySearcher search = new DirectorySearcher();
        search.SearchRoot = de;
        search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + myUser + "))";
        search.PropertiesToLoad.Add("samaccountname");
        search.PropertiesToLoad.Add("thumbnailPhoto");

        SearchResult user;
        user = search.FindOne();

        String userName;
        if (user == null)
            Response.Redirect("app_graphics/user.jpg");
        else
            userName = (String)user.Properties["sAMAccountName"][0];

        try
        {
            byte[] bb = (byte[])user.Properties["thumbnailPhoto"][0];
            Response.BinaryWrite(bb);
            Response.Flush();
        }
        catch
        {
            Response.Redirect("app_graphics/user.jpg");
        }

    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>userImage</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

You can then use the file by passing a parameter of u on a HTTP GET request, containing the users sAMAccountName.

For example, <img src=’userPhoto.aspx?u=gkendal’/>. Also, don’t forget to set the OU of where your user objects are in AD on line 21, and to add a placeholder image for those users that dont have anything stored in thier thumbnailPhoto attribute!

I have been looking at getting a script together for a while to mass change some shortcuts we have on our file server to point to a different location. After some research, and some playing around I found a really easy way to do this in Powershell:


# Call wscript com object
$shell = new-object -com wscript.shell

# Recurse through directories for .lnk files
dir "I:\" -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut($_.fullname)
$oldPath= $lnk.targetPath

# If match text, perform operation
if($oldpath -match "\\serverold\share1")
{
write-host "Match: " + $_.fullname
remove-item $_.fullname
$lnknew = $shell.createShortcut($_.fullname)
$lnknew.targetPath = "`"\\newserver\share1`""
$lnknew.IconLocation = "%SystemRoot%\system32\SHELL32.dll,4"
$lnknew.Save()
}
}
Write-Host "End..."

I’ve always wondered when looking at C# what get and set methods are used for.  In basic terms I found that the get and set method was mainly for reading (get) or writing (set) a property.  After scratching my head for some time, I tried to give it a go as I have problems setting a string to a particular value within one of my C# projects.

I setup another class within my project called “tgtNotes”, which would contain my get and set:

class tgtNotesJK
{
private string LocNotes = null;
public string NotesLoc
{
get
{
return LocNotes;
}
set
{
LocNotes = value;
}
}

I then thought, “surely it’s not that easy”!  I then went back to the code on the form, and defined the class within the code for button1:

tgtNotesJK notestgt = new tgtNotesJK();

Getting and setting is really easy.  To set a value:

notesgt.NotesLoc = "Some text";

And to get the value for example:

Console.WriteLine(notesgt.NotesLoc);

Another great benefit of this is that you will be able to access the methods from anywhere within the C# project.  So because I ‘setted’ a value within button1, will mean for example that I can still access the value within other parts (for example button2).

I’ve got a c# app that I’ve developed, and am now trying to compile on my new system I’ve had a few problems, biggest one being at the line: ‘NotesSession session = new NotesSession();’ ….I’m sure it compiled ok on my last system..hm!

I get the error….CLSID {blahblah} failed with error 80040154.

Turns out that Visual studio was compiling for “Any CPU”, this needs changing to x86, as the notes COM objects don’t do the x64 shizzle. All makes sense now, as new laptop is 64-bit!!

App now works and I’m a bit happier!