Tuesday, November 29, 2011

Storing Pictures in Active Directory


PowerTip of the Day, from PowerShell.com:

When you need to store a picture into an AD account, the picture will have  to be converted to byte values before it can be stored. Just make sure you adjust the path to the picture you want to store and the LDAP path of the AD object you want the picture to be stored in:


  1: # adjust picture path:
  2: $file = "C:\pic.jpg"
  3: $bild = New-Object system.Drawing.Bitmap($file)
  5: $ms = New-Object IO.MemoryStream
  6: $bild.Save($MS, 'jpeg')
  7: $MS.Flush()
  8: $byte = $MS.ToArray()
  9: # adjust user LDAP path:
 10: $user = New-Object System.DirectoryServices.DirectoryEntry`
 12: ("LDAP://,CN=Users,DC=powershell,DC=local")
 14: $User.Properties["jpegPhoto"].Value = $byte



Thanks to www.PowerShell.com

No comments:

Post a Comment