0 votes

Hi, in our Create User action we have setup a custom binary attribute to upload the staff picture.

Is there a way to save the uploaded image to a network share and rename the image as firstname.lastname.jpg ?

We would then trigger a custom command that will upload the image to AD and O365 from the share. We have the custom command to upload the pictures configured, just trying to figure out how to save the uploaded image to the share with proper file name.

Thank you.

by (140 points)
edited by
0

Hello,

Do we understand correctly that you need to set user pictures in AD and Microsoft 365 from a custom binary attribute and saving the picture to a file share is just an intermediary step? Is saving to the share actually required or directly setting the pictures in AD and Microsoft 365 will also work fine?

Any additional details regarding the desired behavior will be much appreciated.

0

Hi, saving to the file share is just an intermediary step and can be avoided if we are able to directly upload the pictures to AD and M365 directly. I have a custom command to upload pictures from a file share, but if there is a way to directly upload the pictures without saving it to the share that would be appriciated.

0

Hello,

Thank you for specifying. For us to provide you with the script, please, specify the version of Adaxes you are currently using. For information on how to check that, have a look at the following help article: https://www.adaxes.com/help/CheckServiceVersion.

0

Hi, the Product Version : 3.14.18920.0

1 Answer

+1 vote
by (292k points)
selected by
Best answer

Hello,

Thank you for the provided details. Please, try the below script. It should be executed in a custom command, business rule or scheduled task configured for the User object type. In the script:

  • $copyFromPropertyName - Specifies the LDAP name of the Binary attribute storing the picture to be set.
  • $photoAttribute - Specifies the LDAP name of the attribute that will be updated with the picture stored in the attribute specified in the $copyFromPropertyName variable.
$copyFromPropertyName = "adm-CustomAttributeBinary1" # TODO: modify me
$photoAttribute = "thumbnailPhoto" # TODO: modify me

function ResizePhoto ($photoBytes, $maxSize, $reduceSizeStep, $reduceSizeInPercents, $maxWidth, $maxHeight)
{
    if ($reduceSizeInPercents -ge 100)
    {
        return ,$photoBytes
    }

    try
    {
        # Calculate the new size, preserve ratio
        $original = [System.Drawing.Image]$photoBytes

        $width = $original.Width
        $height = $original.Height

        if ($NULL -ne $maxWidth)
        {
            if ($width -le $maxWidth -and $height -le $maxHeight)
            {
                return ,$photoBytes
            }

            $ratioX = $maxWidth / $width
            $ratioY = $maxHeight / $height
        }
        else
        {
            $ratioX = ($width - (($width / 100) * $reduceSizeInPercents)) / $width
            $ratioY = ($height - (($height / 100) * $reduceSizeInPercents)) / $height
        }

        $ratio = $ratioY
        if ($ratioX -le $ratioY)
        {
            $ratio = $ratioX
        }

        # Resize the picture
        [int]$newWidth = $width * $ratio
        [int]$newHeight = $height * $ratio

        $newPicture = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
        $graph = [System.Drawing.Graphics]::FromImage($newPicture)

        $graph.Clear([System.Drawing.Color]::White)
        $graph.DrawImage($original, 0, 0, $newWidth, $newHeight)

        $memoryStream = New-Object System.IO.MemoryStream
        $newPicture.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Jpeg)
        $newPictureBytes = $memoryStream.ToArray()
    }
    finally
    {
        # Release resources
        if ($original) { $original.Dispose() }
        if ($graph) { $graph.Dispose() }
        if ($newPicture) { $newPicture.Dispose() }
        if ($memoryStream) { $memoryStream.Dispose() }
    }

    if (($NULL -ne $maxSize) -and 
        ($newPictureBytes.Length -gt $maxSize))
    {
        $reduceSizeInPercents += $reduceSizeStep
        $newPictureBytes = ResizePhoto $photoBytes $maxSize $reduceSizeStep $reduceSizeInPercents
    }

    return ,$newPictureBytes
}

# Get the picture
try
{
    $photoBytes = $Context.TargetObject.Get($copyFromPropertyName)
}
catch
{
    $Context.LogMessage("No picture specified in attribute $copyFromPropertyName.", "Warning")
    return # No picture
}

# Update thumbnailPhoto
$maxSize = 102400
if ($photoBytes.Length -gt $maxSize)
{
    $thumbnailPhotoBytes = ResizePhoto $photoBytes $maxSize 1 0 $NULL $NULL
}
else
{
    $thumbnailPhotoBytes = $photoBytes
}

$Context.TargetObject.Put($photoAttribute, $thumbnailPhotoBytes)
$Context.TargetObject.SetInfo()

# Update Microsoft 365 Photo
try
{
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    $Context.LogMessage("User %fullname% has no account in Microsoft 365.", "Warning")
    return
}

$userPhotoBytes = ResizePhoto $photoBytes $NULL $NULL $NULL 648 648

try
{
    $session = $Context.CloudServices.CreateExchangeOnlinePSSession()
    Import-PSSession $session -CommandName "Set-UserPhoto" 

    # Update the user's photo
    Set-UserPhoto $objectId.ToString() -PictureData $userPhotoBytes -Confirm:$False
}
finally
{
    # Close the remote session
    if ($session) { Remove-PSSession $session }
}
0

Thank you, this does exactly what we wanted to implement.

Cheers!

Related questions

0 votes
1 answer

In a business rule, I'd like to pass Adaxes variables into a powershell script that I'll run. For example, pass %username% into the script so it can be used inside the script.

asked Sep 5 by P-Sysadmin (20 points)
0 votes
1 answer

With Active Directory Users and Computers, I can add group members by copying a list of usernames and pasting them into the Add Members dialog box. This is very quick and easy. How can I do this with Adaxes? It seems that I can only add one member at a time.

asked Feb 24, 2017 by abarker5 (80 points)
0 votes
1 answer

I'm trying to do something very similar to: https://www.adaxes.com/questions/10758/copy-image-uploaded-binary-custom-attribute-network-share but rather than uploading the M365, uploading to ... it, but I'm stuck on this step. I have Adaxes version 3.16.21627.0

asked Oct 28 by jmatthews (190 points)
0 votes
1 answer

I'm in the process of creating a Web interface for requesting IT accounts. Upon submission, I want to run a Powershell script that will create an item in a Sharepoint task list.

asked May 14, 2021 by sandramnc (870 points)
0 votes
1 answer

I need to know how to Create a new Custom Attribute which I wants save some informations of Users

asked Jun 12, 2023 by kanishka.silva (40 points)
3,569 questions
3,259 answers
8,275 comments
547,937 users