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 (270k 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

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 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)
0 votes
1 answer

How do change the display name of a custom attribute in version 2017.2? All the information I am finding contains links that bring me to 2018 version

asked Nov 1, 2018 by hgletifer (1.3k points)
0 votes
1 answer

Hi we have bunch of custom commands that HR uses, to create new user employee or offboard someone. They can see the execution log on the web interface, but we would like to recive a copy of the log in an email to IT to make sure there arent errors etc.

asked Jun 30, 2021 by TJ_Umredkar (140 points)
3,326 questions
3,025 answers
7,723 comments
544,675 users