Skip to content

Commit

Permalink
Merge pull request #599 from petkir/graph-update-user-photo
Browse files Browse the repository at this point in the history
Graph update user photo
  • Loading branch information
pkbullock authored Oct 18, 2023
2 parents a99ebc7 + 6b42a68 commit 66ebe03
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
81 changes: 81 additions & 0 deletions scripts/graph-update-user-photo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
plugin: add-to-gallery
---

# Add or Update User Photo

## Summary

The script streamlines the process of updating user photos of images stored locally. It ensures efficient handling of both successful updates and errors, providing an organized approach to managing user photos within Microsoft 365.

![Example Screenshot](assets/example.png)

## Limitations

Maximum FileSize is 4MB (REST request limit is 4MB)

# [Microsoft Graph PowerShell](#tab/graphps)

Config parameter:
```$imageSourcePath```
```$upnDomain```
name of the image file has to be the username without the domain

|Name | Filename | UPN |
|---------|----------|-----|
|John Doe |j.doe.jpg | j.doe@contoso.com |

```powershell
$upnDomain="@contoso.com"
$imageSourcePath="c:\temp\images\"
$completeFolder="Done"
$errorFolder="Error"
Connect-MgGraph -Scopes "User.ReadWrite"
if(-not(Test-Path $imageSourcePath)){
Write-Host "Image Source Path not exists, Please Check the ConfigValue imageSourcePath"
exit(2)
}
$imageFiles=Get-ChildItem -Path $imageSourcePath -Filter *.jpg
Import-Module Microsoft.Graph.Users
foreach($imageFile in $imageFiles){
$username=$imageFile.Name.Substring(0,$imageFile.Name.lastIndexof('.'));
$content=Get-Content $imageFile.VersionInfo.FileName;
$upn ="$username$upnDomain"
try{
$Error.Clear()
Set-MgUserPhotoContent -UserId $upn -InFile $imageFile.VersionInfo.FileName
if($Error.Count -gt 0){
throw "Error in Set-MgUserPhotoContent"
}
$completePath = Join-Path -Path $imageSourcePath -ChildPath $completeFolder
if(-not(Test-Path $completePath)){
New-Item -ItemType Directory -Path $completePath
}
Move-Item -Path $imageFile.VersionInfo.FileName -Destination $completePath -Force
}catch{
# Write-Host $error
$errorPath = Join-Path -Path $imageSourcePath -ChildPath $errorFolder
if(-not(Test-Path $errorPath)){
New-Item -ItemType Directory -Path $errorPath
}
$Error|Set-Content -Path "$errorPath\$username.error.txt"
Move-Item -Path $imageFile.VersionInfo.FileName -Destination $errorPath -Force
}
}
```
[!INCLUDE [More about Microsoft Graph PowerShell SDK](../../docfx/includes/MORE-GRAPHSDK.md)]
***

## Contributors

| Author(s) |
|-----------|
| [Peter Paul Kirschner](https://github.com/petkir) |

[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/graph-update-user-photo" aria-hidden="true" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions scripts/graph-update-user-photo/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[
{
"name": "graph-update-user-photo",
"source": "pnp",
"title": "Add or Update User Photo",
"shortDescription": "The script streamlines the process of updating user photos of images stored locally. It ensures efficient handling of both successful updates and errors, providing an organized approach to managing user photos within Microsoft 365",
"url": "https://pnp.github.io/script-samples/graph-update-user-photo/README.html",
"longDescription": [
""
],
"creationDateTime": "2023-10-13",
"updateDateTime": "2023-10-13",
"products": [
"Graph"
],
"metadata": [
{
"key": "GRAPH-POWERSHELL",
"value": "1.0.0"
},
{
"key": "POWERSHELL",
"value": "7.2.0"
}
],
"categories": [
"Data",
"Provision",
"Configure"
],
"tags": [
"<Cmdlets-Used>"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/graph-update-user-photo/assets/preview.png",
"alt": "Preview of the sample Add or Update User Photo"
}
],
"authors": [
{
"gitHubAccount": "petkir",
"company": "",
"pictureUrl": "https://github.com/petkir.png",
"name": "Peter Paul Kirschner"
}
],
"references": [
{
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
},
{
"name": "Want to learn more about Microsoft Graph PowerShell SDK and the cmdlets",
"description": "Check out the Microsoft Graph PowerShell SDK documentation site to get started and for the reference to the cmdlets.",
"url": "https://docs.microsoft.com/en-us/graph/powershell/get-started"
}
]
}
]

0 comments on commit 66ebe03

Please sign in to comment.