Skip to content

Instantly share code, notes, and snippets.

@bradmb
Created February 26, 2015 04:17
Show Gist options
  • Save bradmb/3961b39f7c4beec7a5fb to your computer and use it in GitHub Desktop.
Save bradmb/3961b39f7c4beec7a5fb to your computer and use it in GitHub Desktop.
Query Active Directory (using sAMAccountName) and Return A Thumbnail Photo
open System.DirectoryServices
/// Gets a user's thumbnail photo from active directory. Returns null if no image found.
let ActiveDirectoryImageFor(samAccount:string):byte[] =
use adSearcher = new DirectorySearcher()
adSearcher.Filter <- "(&(objectClass=user) (sAMAccountName=" + samAccount + "))"
let adResult = adSearcher.FindOne()
if adResult = null
then
null
else
use adUser = new DirectoryEntry(adResult.Path)
let adJpeg = adUser.Properties.["thumbnailPhoto"]
let adImageData = if (adJpeg.Value :? byte[]) then adJpeg.Value :?> byte[] else null
adImageData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment