WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADSI LDAP CDO
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Converting Active Directory Thumbnailphotos to File

 Keywords: Converting Active Directory Thumbnailphotos File Binary 

Question:

In our AD environment we have stored small images of some of our users in the thumbnailphoto user object attribute. AD stores the bitmap as a String, with each byte represented by a number (0-255) separated by a space in PowerShell I can retrieve this attribute and write it to a file with the set-content $path -encoding byte sample powershell:
   
   $user = Get-ADUser someuser -properties SamAccountName,ThumbnailPhoto
   $user.thumbnailphoto | Set-Content ("c:\ADexport\Photos\"+$User.samaccountname+".jpg") -Encoding byte
I am trying to duplicate this behavior in Winbatch, the following code succeeds but is EXTREMELY inefficient
  If WinMetrics(-2) == 3 Then AddExtender("WWADS64I.DLL") ; 64-bit
  Else AddExtender("WWADS44I.DLL") ; 32-bit

ADSIPath = dsFindPath("LDAP://xxx.xxx.loc","(samaccountname=someuser)")
Photo = dsgetproperty(ADSIPath,"thumbnailphoto")
StrBytes = ItemCount(Photo," ")
Outfile = "C:\ADExport\Photos\" : dsgetproperty(ADSIPath,"samaccountname") : "-winbatch.jpg"
binBuf = BinaryAlloc(StrBytes+100)
For i = 1 to StrBytes
   BinaryPoke(BinBuf,i-1,itemextract(i,Photo," "))
Next I

BinaryWrite( BinBuf, OutFile )         ; Writes a binary buffer to a file.
BinaryFree(BinBuf)
I have tons of experience with winbatch going back years, however I don't often work with bin buffers, so perhaps there is a much more efficient way to convert the string into a binary file. Does anyone have some ideas on this?

Example of string as exist in AD (this is the full string and when converted and written to a file, would be a picture)

255 216 255 224 0 16 74 70 73 70 0 1 1 1 0 96 0 96 0 0 255 225 0 90 69 120 105 102 0 0 77 77 0 42 0 0 0 8 0 5 3 1 0 5 0 0 0 1 0 0 0 74 3 3 0 1 0 0 0 1 0 0 0 0 81 16 0 1 0 0 0 1 1 0 0 0 81 17 0 4 0 0 0 1 0 0 14 196 81 18 0 4 0 0 0 1 0 0 14 196 0 0 0 0 0 1 134 160 0 0 
177 143 255 219 0 67 0 2 1 1 2 1 1 2 2 2 2 2 2 2 2 3 5 3 3 3 3 3 6 4 4 3 5 7 6 7 7 7 6 7 7 8 9 11 9 8 8 10 8 7 7 10 13 10 10 11 12 12 12 12 7 9 14 15 13 12 14 11 12 12 12 255 219 0 67 1 2 2 2 3 3 3 6 3 3 6 12 8 7 8 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 255 192 0 17 8 0 96 0 96 3 1 34 0 2 17 1 3 17 1 255 196 0 31 0 0 1 5 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 255 196 0 181 16 0 2 1 3 3 2 4 3 5 5 4 4 0 0 
1 125 1 2 3 0 4 17 5 18 33 49 65 6 19 81 97 7 34 113 20 50 129 145 161 8 35 66 177 193 21 82 209 240 36 51 98 114 130 9 10 22 23 24 25 26 37 38 39 40 41 42 52 53 54 55 56 57 58 67 68 69 70 71 72 73 74 83 84 85 86 87 88 89 90 99 100 101 102 103 104 105 106 115 116 
117 118 119 120 121 122 131 132 133 134 135 136 137 138 146 147 148 149 150 151 152 153 154 162 163 164 165 166 167 168 169 170 178 179 180 181 182 183 184 185 186 194 195 196 197 198 199 200 201 202 210 211 212 213 214 215 216 217 218 225 226 227 228 229 230 231 
232 233 234 241 242 243 244 245 246 247 248 249 250 255 196 0 31 1 0 3 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 255 196 0 181 17 0 2 1 2 4 4 3 4 7 5 4 4 0 1 2 119 0 1 2 3 17 4 5 33 49 6 18 65 81 7 97 113 19 34 50 129 8 20 66 145 161 177 193 9 35 51 
82 240 21 98 114 209 10 22 36 52 225 37 241 23 24 25 26 38 39 40 41 42 53 54 55 56 57 58 67 68 69 70 71 72 73 74 83 84 85 86 87 88 89 90 99 100 101 102 103 104 105 106 115 116 117 118 119 120 121 122 130 131 132 133 134 135 136 137 138 146 147 148 149 150 151 152 
153 154 162 163 164 165 166 167 168 169 170 178 179 180 181 182 183 184 185 186 194 195 196 197 198 199 200 201 202 210 211 212 213 214 215 216 217 218 226 227 228 229 230 231 232 233 234 242 243 244 245 246 247 248 249 250 255 218 0 12 3 1 0 2 17 3 17 0 63 0 253 
ETC...

Answer:

A mix of the ADSI extender and a little COM should speed things up quite a bit. Here is an example
ADSIPath = dsFindPath("LDAP://xxx.xxx.loc","(samaccountname=someuser)")
Outfile = "C:\ADExport\Photos\" : dsgetproperty(ADSIPath,"samaccountname") : "-winbatch.jpg"

objUser = ObjectGet(AdSIPath)
Photo   = objUser.Get("thumbnailphoto")
objUser = 0
hPhoto  = BinaryAllocArray( Photo )
BinaryWrite( hPhoto , OutFile )       
BinaryFree(hPhoto )
Here is a pure COM sample:
; Bind to the user object in Active Directory with the LDAP provider.
objUser = ObjectGet("LDAP://" : strUserDN)
Photo   = objUser.Get("thumbnailphoto")
objUser = 0
hPhoto  = BinaryAllocArray( Photo )
BinaryWrite( hPhoto , OutFile )       
BinaryFree(hPhoto )

Article ID:   W18050
Filename:   Converting Active Directory Thumbnailphotos to File .txt
File Created: 2013:10:29:08:06:46
Last Updated: 2013:10:29:08:06:46