Add Photo to ADUser from csv with Base64Data

Welcome Forums General PowerShell Q&A Add Photo to ADUser from csv with Base64Data

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #112150

    Participant
    Points: 0
    Rank: Member

    Community!

    I'm looking for a way to take a CSV full of Base64Image data and add it to the AD User profiles.

    I'm not sure of the size of each file either so I will need to resize to something AD will accept in the script.

    Any help would be appreciated.

     

    Thanks for your time and patience.

  • #112156

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

    You don't say what you have tried or search for. We are here to help, but asking folks to do your work for you is why consultants exist. 8^}

    So, you have a CSV Base64Image data  strings, not real pictures, meaning created using other tool?

    ADDS will only take pictures of a given size, and this is documented in the MS TechNet/MSDN documentation and many blogs all over the web.

    So, you need to know the file size of each image (recommended thumbnail photo size is 96×96 pixels and maximum size of photo that can be imported with the Import-RecipientDataProperty command is 10-KB), and if it does not meet the ADDS specifications, you need to fix that.

    There are many examples and even pre-built script showing how to import images in to ADDS, for example:

    https://gallery.technet.microsoft.com/How-to-Import-Employee-0058e89b

    https://social.technet.microsoft.com/Forums/windows/en-US/cf3dd459-8cc4-47ed-8772-3364f9bfcd6d/how-to-import-photos-into-active-directory

    There are many similar locations taking to converting image to base64, and we can only image some of there tools were used or done say this way...

    # Convert image to base64
    $b64 = [convert]::ToBase64String((Get-Content 'F:\Pictures\SecureZip.jpg' -encoding byte))
    

    All that being said, converting and resizing on the fly, that is a different process

    https://adminscache.wordpress.com/2013/06/09/base64-encoding-of-images

    Converting, base64 string back to a file, say, using something like...

    The FromBase64String() method converts a base64-encoded string to a byte array. All you need to do is write that byte array back to a file:

    # Path to new image file
    $filename = 'D:\temp\imagefile.jpg'
    
    # Convert base64 image back to image file
    $bytes = [Convert]::FromBase64String("$b64")
    [IO.File]::WriteAllBytes($filename, $bytes)

    Then resizing that image using say this:

    https://gallery.technet.microsoft.com/scriptcenter/Resize-Image-A-PowerShell-3d26ef68

  • #112793

    Participant
    Points: 0
    Rank: Member

    Ok, apologies for just shooting it out there. Not used to asking on the forum.

    I am stuck trying to get a CSV dump of EmployeeID numbers along with the Base64String of their pictures from our HRIS system and then importing them into AD.

    So I have 2 separate pieces I'm guessing.

    One looks something like

     

    Import-Csv-Path".\ActiveDirectory_WorkerPhotos_1.csv"|foreach {

    $filename=".\data\fake.jpg"

    $bytes=[Convert]::FromBase64String($_.Base64Image)

    [IO.File]::WriteAllBytes($filename,$bytes)

    Resize-Image-InputFile$filename-Width96-Height96-Display

    Set-ADUser-Replace @{thumbnailPhoto=([byte[]](Get-Content$filename-Encodingbyte))} $_.username-FilePath$filename

    }

    I know, it's a mess but I'm at the ragged edge of my PS knowledge as of now so....

     

     

     

     

     

     

     

  • #112810

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

    As for this...

    ...
    I am stuck trying to get a CSV dump of EmployeeID numbers along with the
    Base64String of their pictures from our HRIS system
    ...

    I cannot help you with this one=, since I have no idea what it is. You must get:

    1. this into a CSV
    2. Write each picture to a real file
    3. resize those files
    4. then import to ADDS
    # Export data from HRIS as a file. this file contains username
    # and the Base64String of their picture. 
    #  Username,UserPicture
    
    .\hrisuserpictures.csv
    
    Import-Csv-Path".\hrisuserpictures.csv" | 
    foreach {
        $filename = ".\$($_.Username).jpg"
        $bytes = [Convert]::FromBase64String($($_.UserPicture))
        [IO.File]::WriteAllBytes($filename,$bytes)
    
        Resize-Image -InputFile $filename -Width 96 -Height 96 -Display
        Set-ADUser-Replace @{thumbnailPhoto = ([byte[]](Get-Content -Path $filename -Encodingbyte))} $_.username -FilePath$filename
    }
    

    Of course I have no way of testing this, as my environment does not use pictures.
    Well, that and that spending time trying to recreate what your HRIS system would provide,
    is not really reproduceable thing.

You must be logged in to reply to this topic.