Po$H Pete | Those who can… $cript
13May/111

Get and Set – Active Directory User Thumbnail Photos

I was just asked how you can update the user thumbnail images in active directory (the ones that appear in outlook when you select a user etc). So I did some digging.

Basically, thumbnailPhoto is a property of an AD user which stores an array of bytes. So I've put together the following functions to allow you to easily get or set the images for a user:

Function Get-ADThumbnailPhoto()
{
<#
.Synopsis
Get Active Directory user thumbnail image
.Description
Extracts the current user thumbnail image from active directory
.Parameter UserName
The username of the person you're looking for
.Parameter Path
The path for the file you would like to output to, i.e. c:\test.jpg
.Example
Get-ADThumbnailPhoto -UserName PeterRossi -Path c:\PeterRossi.JPG
 
This will extract the thumbnail image for user PeterRossi and export it to c:\PeterRossi.jpg
.Notes
Name: Get-ADThumbnailPhoto
Author: Peter Rossi
Last Edited: 13th May 2011
#>
param(
        [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$False)]
        [string[]]$UserName,
		[Parameter(Mandatory=$True, ValueFromPipeline=$false)]
        [String]$Path
	)
	$ADSearcher = new-object DirectoryServices.DirectorySearcher("(&(SAMAccountName=$UserName))")
	$Users = $ADSearcher.FindOne()
 
	if($Users -ne $null)
	{
		[adsi]$TheUser = "$($Users.Path)"
		$Thumbnail = $TheUser.ThumbnailPhoto.Value
		[System.IO.File]::WriteAllBytes($Path,$Thumbnail)	
	}
	Else
	{
		Write-Warning "User $UserName could not be found in AD, is it the right username?"
	}
}
 
Function Set-ADThumbnailPhoto()
{
<#
.Synopsis
Set Active Directory user thumbnail image
.Description
Sets the current user thumbnail image from active directory
.Parameter UserName
The username of the person you're looking for
.Parameter JPGPath
The path for the file you would like to use as the new thumbnail
.Example
Set-ADThumbnailPhoto -UserName PeterRossi -JPGPath c:\PeterRossi.JPG
 
This will set the thumbnail image for user PeterRossi using c:\PeterRossi.jpg
.Notes
Name: Set-ADThumbnailPhoto
Author: Peter Rossi
Last Edited: 13th May 2011
#>
param(
        [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$False)]
        [string[]]$UserName,
		[Parameter(Mandatory=$True, ValueFromPipeline=$false)]
        [String]$JPGPath
	)
 
	if((Test-Path $JPGPath) -eq $true)
	{
		[byte[]]$Thumbnail = Get-Content $JPGPath -encoding byte
 
		$ADSearcher = new-object DirectoryServices.DirectorySearcher("(&(SAMAccountName=$UserName))")
		$UserSearch = $ADSearcher.FindOne()
 
		if($UserSearch -ne $null)
		{
			$User = [ADSI]"$($UserSearch.Path)"
 			$User.put("thumbnailPhoto",  $Thumbnail )
			$User.setinfo()
		}
		Else
		{
			Write-Warning "User $UserName could not be found in AD, is it the right username?"
		}
	}
	Else
	{
		Write-Warning "Can't find $JPGPath, does it exist?"
	}
}
Comments (1) Trackbacks (0)
  1. Hi i’m new to running scripts…mostly run commands. :) I have copied the Get-ADThumbnailPhoto into a script on my system and use the switches that you defined for username and path but it still gives me errors. I run Get-ADThumbnailPhoto -username brian -path c:\scripts\image.jpg from the directory that has the Get-ADThumbnailPhoto script and it tells me:

    The term ‘Get-ADThumbnailPhoto’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was inc
    luded, verify that the path is correct and try again.
    At line:1 char:21
    + Get-ADThumbnailPhoto <<<< -username brian -path c:\scripts\image.jpg
    + CategoryInfo : ObjectNotFound: (Get-ADThumbnailPhoto:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Any help? By the way I haven't even started playing with setting the photo and took that portion out of the script.

    Thanks
    Brian


Leave a comment

No trackbacks yet.