http://qs321.pair.com?node_id=913943

bingos has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am having trouble with updating the thumbnailPhoto Active Directory attribute using the ADSI interface through Win32::OLE

I have been able to add photos using an Active Directory Users and Computers extension to individual user accounts, but I would like to have a way of doing 'bulk' imports. And of course my language of choice is perl for doing this.

The thumbnailPhoto attribute is an 'octet string' and I don't believe I've tried to manipulate this type of attribute before, though, I have retrieved values from them without any problems.

First off, I thought I would try and export images that had been previously imported:

use strict; use warnings; use Win32::OLE qw[in]; my $adspath = shift || die "Provide an adspath already\n"; { warn $adspath, "\n"; my $user = Win32::OLE->GetObject("LDAP://$adspath"); die "Oh dear\n" unless $user; $user->GetInfo; unless ( defined $user->{thumbnailPhoto} ) { warn "No thumbnail\n"; exit 0; } my $thumb = $user->{thumbnailPhoto}; { open my $piccy, '>:raw', 'piccy.jpg' or die "$!\n"; print $piccy $thumb; } } exit 0;

This works fine to retrieve a 'piccy' and I had a JPEG file I could open and admire.

Buoyed up by the success of this, I tried this:

use strict; use warnings; use File::Slurp; use Win32::OLE qw[in]; my $adspath = shift || die "Provide an adspath already\n"; my $filename = shift || die "No filename provided\n"; my $content = read_file( $filename, binmode => ':raw' ); { warn $adspath, "\n"; my $user = Win32::OLE->GetObject("LDAP://$adspath"); die "Oh dear\n" unless $user; $user->GetInfo; $user->Put('thumbnailPhoto',$content); $user->SetInfo; } exit 0;

This ends up with 'ÿØÿà' in the attribute.

I tried various things, including:

The above seemed to either have no effect (Variant) or put garbage in the attribute

I found VBScript code that seemed to use a byte array to assign to the attribute:

Function ReadBinaryFile(FileName) Const adTypeBinary = 1 Dim BinaryStream Set BinaryStream = CreateObject("ADODB.Stream") BinaryStream.Type = adTypeBinary BinaryStream.Open BinaryStream.LoadFromFile FileName ReadBinaryFile = BinaryStream.Read End Function Set objNewUser = GetObject("LDAP://cn=some,ou=adspath,dc=domain,dc=loc +al") objNewUser.GetInfo objNewUser.Put "thumbnailPhoto", ReadBinaryFile("image.jpg") objNewUser.SetInfo

I have an idea what a byte array is, but not how to coerce one from the contents of a Perl scalar. So I am stuck and would be grateful of any advice.

Many thanks in advance.