2010-07-01

Updating pictures for Users in AD - pt. 2

The last sentence in my previous post on Updating Pictures for Users in AD was

Now the only part hat needs to be taken care of - is getting a proper image from HR in the correct size. Maybe we will find a Powershell script to take care of this :)

So here it is.

This can be done with ImageMagick

Once you have unpacked the Zip file (to c:\temp\ for example)

Function convert-pics {
	$cmd = "C:\temp\ImageMagick-6.6.2-5\convert.exe"
	Get-ChildItem C:\temp\pics | ForEach-Object { 
	$newname = "C:\temp\pics\test\"+$_.Name
	$filename = $_.Fullname
	invoke-expression "$cmd $Filename -resize 96x96 $newname"
	}
}

convert-pics

 

Line 2. Define the command you will run

Line 4. Save the files to different folder

Line 6. Run the command to convert the pictures to the correct size.

And uploading them all to Active Directory

 

Get-QADUser -sizelimit 0 -IncludedProperties  thumbnailphoto |  ForEach-Object {
	if ($_.thumbnailPhoto -notlike "FF*") {
		$filename = "C:\temp\pics\test\"+$_.SamAccountName+".jpg"
		if (Test-Path $filename) {
			$photo = [byte[]](Get-Content $filename -Encoding byte)
			Set-QADObject -identity $_.SamAccountName -ObjectAttributes @{thumbnailPhoto=$photo}
		}	
	}
}	

 

Line 1. You have to specifically define the thumbnailPhoto attribute to be retrieved

Line 2. Check if the user already has the attribute populated already - if not then continue

Line 3. Set the variable of the filename

Line 4. Check for the file - if it exists then continue.