Uploading a Jpeg into AD

This final function will upload a suitably sized Jpeg into AD. It’s not as simple as loading the jpeg into the thumbnail attribute you have to byte encode it. This is however as easy as casting the variable type to an array of bytes ( variable type [byte[]] ) and letting PowerShell do the heavy lifting for you as always.

The only two lines of code you need are the [byte[]]$jpg = Get-Content $jpegFile -encoding byte and the Set-QADUser -Identity $guid -ObjectAttributes @{thumbnailPhoto=$jpg

I’ve used the GUID to identify the target user but you can use anything in your version of the function obviously. The function also writes to a log file for you.

This was quite an early script for me and I’ve not set the parameters to be mandatory or error checked them, see my other posts on creating fucntions ( this probably breaks all my recommendations 🙂 ) The function just assumes the parameters are always going to be correct. I’d recommend you add some error handling and these functions are probably due a rewrite but as the 3 functions I just posted are currently part of a much larger script I don’t have to worry about the error handling as I know what’s being passed to each function but it’s still good practice to add error handling in all of your functions then you can just copy and paste them into other scripts safe in the knowledge that they will work and deal with all errors.

Function Update-Thumbnail {
 param (
# check if the GUID was passed as a parameter - if not we can't upload the picture
 if ( (!($guid)) -or ( $guid.Length -le 0) ) {Return $false	}
# convert the picture file - byte encoded
 if ($jpg) { if ($jpg -is [IDisposable]) {try {$jpg.Dispose() | Out-Null } catch{}}; Remove-Variable -Name jpg -ErrorAction SilentlyContinue | Out-Null }
 Try { [byte[]]$jpg = Get-Content $jpegFile -encoding byte } Catch{}
 If ( $jpg )	{
 $logAction = " thumbnail updated "
 Try {Set-QADUser -Identity $guid -ObjectAttributes @{thumbnailPhoto=$jpg} -ErrorAction SilentlyContinue | Out-Null } Catch{$logAction = " thumbnail failed to update " } 
  $($(Get-Date).tostring() + " " + $(RPAD -stringToPad $sAMAccountName -paddedLength 20) + $logAction )| out-file $logfile -append -encoding default
 if ($jpg) { if ($jpg -is [IDisposable]) {try {$jpg.Dispose() | Out-Null } catch{}}; Remove-Variable -Name jpg -ErrorAction SilentlyContinue | Out-Null }
 Return $true

One thought on “Uploading a Jpeg into AD

  1. […] to convert the format to a  jpeg  then resize it to fit into the AD size limit and then finally how to upload it into AD in the […]

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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.