The script gets a user photo from Microsoft 365 and sets it into the AD account of the user. To run the script, create a custom command or scheduled task configured for the User object type.
PowerShell
# Get user ID in Microsoft 365
try
{
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
return
}
try
{
# Connect to Exchange Online
$session = $Context.CloudServices.CreateExchangeOnlinePSSession()
Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Get-UserPhoto"
# Get user photo
$picture = Get-UserPhoto $objectId.ToString() -ErrorAction SilentlyContinue
}
finally
{
# Close the remote session and release resources
if ($session) { Remove-PSSession $session }
}
if ($NULL -eq $picture.PictureData)
{
return
}
# Update user photo in AD
$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath, $True)
$user.Put("thumbnailPhoto", $picture.PictureData)
$user.SetInfo()
What exactly do you mean? Could you, please, describe the desired behavior in all the possible details with live examples?