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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.