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 }
}