Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Importing a jpeg into an AD attribute with Win32::OLE

by bingos (Vicar)
on Jul 12, 2011 at 15:53 UTC ( #913943=perlquestion: print w/replies, xml ) Need Help??

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:

  • Win32::OLE::Variant - Variant( VT_ARRAY|VT_UI1, $content )
  • pack and unpack
  • Sacrificial slaughter

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.

Replies are listed 'Best First'.
Re: Importing a jpeg into an AD attribute with Win32::OLE
by jand (Friar) on Jul 19, 2011 at 01:13 UTC
    I don't have a compatible LDAP server to test with, so I can't experiment. Could you try using the ReadBinaryFile() function above translated into Perl:
    sub ReadBinaryFile { my $filename = shift; my $stream = Win32::OLE->new("ADODB.Stream"); $stream->{Type} = 1; # adTypeBinary $stream->Open; $stream->LoadFromFile($filename); my $retval = Variant(); $stream->Dispatch("Read", $retval); return $retval; }
    The last 3 lines in the function are written in a way to preserve the VT_UI1|VT_ARRAY variant exactly as it was returned from the stream reader.

    In the code you were using, did the $content have the UTF8 flag set? That's about the only thing I can think of right now that would mess up the conversion from a Perl string to a COM byte array.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://913943]
Approved by Corion
Front-paged by MidLifeXis
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-03-01 16:26 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (13 votes). Check out past polls.