Script repository
The script gets a user photo from Microsoft 365 and sets it into the AD account of the user. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.
# Get user ID in Microsoft 365.
$azureID = $Context.TargetObject.AzureID
if ($NULL -eq $azureID)
{
$Context.LogMessage("The user does not have a Microsoft 365 account", "Warning")
return
}
# Connect to Microsoft Graph PowerShell.
$accessToken = $Context.CloudServices.GetAzureAuthAccessToken()
Connect-MgGraph -AccessToken ($accessToken | ConvertTo-SecureString -AsPlainText -Force)
# Get temp file path.
$tempFodlerPath = [System.IO.path]::GetTempPath()
$tempFilePath = "$tempFodlerPath%sAMAccountName%.tmp"
# Download user photo.
try
{
Get-MgUserPhotoContent -UserId $azureID -OutFile $tempFilePath -ErrorAction Stop
}
catch
{
$Context.LogMessage("The user has no photo", "Warning")
return
}
$photoBytes = [System.IO.File]::ReadAllBytes($tempFilePath)
if ($NULL -ne $photoBytes)
{
# Update user photo in AD.
$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath, $True)
$user.Put("thumbnailPhoto", $photoBytes)
$user.SetInfo()
}
# Remove temp file.
Remove-Item $tempFilePath -Force -Confirm:$False
Comments 13
You must be signed in to comment.
A
I have this setup but is there a way to do a bulk import from 365 for all users
Support
Hello,
What exactly do you mean? Could you, please, describe the desired behavior in all the possible details with live examples?
Trent
We would like to do this for all users, is that possible?
Support
Hello Trent,
As it is mentioned in the script description, it can be executed in a scheduled task or custom command. As such, the easiest option is to create a one-time task executing the script and add All objects to the Activity Scope.
Kevin
Hi,
Microsoft annouched they will stop the get-userphoto cmdlet from Exchange Online. Will you be upgrading the script to the Microsoft Graph PowerShell ((Updated) ExchangePowerShell: retirement of tenant admin cmdlets to Get, Set, and Remove UserPhotos)?
Support
Hello Kevin,
The script uses the Get-MgUserPhotoContent cmdlet from the Microsoft.Graph PowerShell module to get the photo. Please, note that for the script to work, the module must be installed on the computer where the Adaxes service runs. If you have multiple Adaxes services sharing a common configuration, the module must be installed on each computer where the Adaxes service runs.
Kevin
Awesome then we don't have to fix anything for the upcomming update of the powershell cmdlets for Exchange.
Andrew Baker
Hi,
I've just tried to run this on my tenant and got the following error?
Is there a particular version of the Graph modules required?
---------------------------
Run Script - User McUser
---------------------------
[UnknownError] : Stack trace: at Get-MgUserPhotoContent, C:\Program Files\WindowsPowerShell\Modules\Microsoft.Graph.Users\2.26.1\exports\ProxyCmdletDefinitions.ps1: line 12253
at , : line 21
---------------------------
Support
Hello Andrew,
> I've just tried to run this on my tenant and got the following error?
How exactly did you execute the script? Was it in Windows PowerShell?
For troubleshooting purposes, please, send us (support@adaxes.com) a screenshot of the error you are facing.
>Is there a particular version of the Graph modules required?
It is only required to have a version of the module compatible with PowerShell 5. As of now, Adaxes does not support PowerShell 7.
Marcel
Hi, we have changed this line in the script:
to
This because we had some issues where the photo.tmp was not correct downloaded or deleted. And then the wrong photo (of a different user) was added to AD (and syned to the MS365 account).
Please make this change to avoid this problem.
Support
Hello Marcel,
Thank you for the suggestion. We updated the script accordingly.
Ilia Goldenberg
AD attribute "thumbnailPhoto" has hard limits and size recommendations. The maximum size limit of this attribute is 100KB, while recommended size is ~10KB. This attribute is also has direct impact on AD replication (total size of replicated data)...
On the other hand, the photo downloaded by Get-MgUserPhotoContent might be larger.
Current version of script do not performs size validation.
I would suggest to update the script to something like this (tested only on single user):
Support
Hello Ilia,
Thank you for the suggestion. We will consider updating the script to include resizing.