Quantcast
Viewing all articles
Browse latest Browse all 37

Manage Active Directory photos with Powershell

Batch uploading photos to Active Directory 

Hello guys!

We Just started to implement Lync server. And I thought that would be great to see thumbnail pictures in Lync messenger of our organization users. The same attribute using Outlook 2010/2007 to show user photos.Our situation: Each night we uploading photos to AD jpegPhoto attribute. So my mission is to download uploaded photos, convert them to appropriate size (The optimal dimensions for the GAL photos is 96x96 pixels)  and upload them back to AD  thumbnailphoto attribute. 


Script which downloads photos from active directory jpegPhoto attribute.  
Change photo path by your need in set-content "c:\temp\$sAMAccountName.jpg"

$filter = '(&(objectCategory=Person)(objectClass=User))'
$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$searcher = New-Object System.DirectoryServices.DirectorySearcher $filter
$searcher.findall()  | foreach {
 $user = [adsi]$_.path
 $sAMAccountName = $user.psbase.Properties["samaccountname"]
 $user.psbase.Properties["jpegPhoto"] | set-content "c:\temp\$sAMAccountName.jpg" -Encoding byte
}

For converting photos, I used GALBatchConvert.ps1 script that uses ImageMagick, you can find it at http://www.stevieg.org/tag/optimization/ site
Start Powershell and give command .\GALBatchConvert.ps1 -InputFolder c:\temp -OutputFolder c:\temp\converted

p.s. I got error for non ASCII symbols, so look at your sAMAccountName's if they have one.

We have converted photos, now we need to upload back to AD thumbnailPhoto attribute:
 Change $jpgfile path by your need
$filter = '(&(objectCategory=Person)(objectClass=User))'
$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$searcher = New-Object System.DirectoryServices.DirectorySearcher $filter
$folder = "c:\temp\converted\"
$searcher.findall() | foreach {
    $user = [adsi]$_.path
    $sAMAccountName = $user.psbase.Properties["samaccountname"]
    $jpgfile = "c:\temp\converted\$sAMAccountName.jpg"
    [byte[]]$jpg = Get-Content $jpgfile -encoding byte
    $user.psbase.Properties["thumbnailPhoto"].Clear()
    $null = $user.psbase.Properties["thumbnailPhoto"].Add($jpg)
    $user.psbase.commitChanges()
}


Good luck!

Viewing all articles
Browse latest Browse all 37

Trending Articles