-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Get-PnPUserProfilePhoto cmdlet
- Loading branch information
1 parent
ab2c5d2
commit 8bc7508
Showing
4 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
Module Name: PnP.PowerShell | ||
title: Get-PnPUserProfilePhoto | ||
schema: 2.0.0 | ||
applicable: SharePoint Online | ||
external help file: PnP.PowerShell.dll-Help.xml | ||
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPUserProfilePhoto.html | ||
--- | ||
|
||
# Get-PnPUserProfilePhoto | ||
|
||
## SYNOPSIS | ||
|
||
**Required Permissions** | ||
|
||
* Microsoft Graph API: One of ProfilePhoto.ReadWrite.All, User.ReadWrite or User.ReadWrite.All | ||
|
||
Gets the profile picture of a user. | ||
|
||
## SYNTAX | ||
|
||
```powershell | ||
Get-PnPUserProfilePhoto -Identity <AzureADUserPipeBind> [-Filename <String>] [-Connection <PnPConnection>] | ||
``` | ||
|
||
## DESCRIPTION | ||
This cmdlet downloads the user profile photo to the specified path and filename. If no filename has been specified it will default to the Display Name of the user with the either the extension .png or .jpeg depending on the format of the file. | ||
|
||
## EXAMPLES | ||
|
||
### EXAMPLE 1 | ||
```powershell | ||
Get-PnPUserProfilePhoto -Identity "john@contoso.onmicrosoft.com" | ||
``` | ||
Downloads the photo for the user specified to the current folder. | ||
|
||
### EXAMPLE 2 | ||
```powershell | ||
Get-PnPUserProfilePhoto -Identity "john@contoso.onmicrosoft.com" -Filename "john.png" | ||
``` | ||
Downloads the photo for the user specified to the current folder and will name the file 'john.png'. | ||
|
||
## PARAMETERS | ||
|
||
### -Connection | ||
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. | ||
|
||
```yaml | ||
Type: PnPConnection | ||
Parameter Sets: (All) | ||
|
||
Required: False | ||
Position: Named | ||
Default value: None | ||
Accept pipeline input: False | ||
Accept wildcard characters: False | ||
``` | ||
### -Filename | ||
The path to the image file to save. | ||
```yaml | ||
Type: String | ||
Parameter Sets: (All) | ||
|
||
Required: False | ||
Position: Named | ||
Default value: None | ||
Accept pipeline input: False | ||
Accept wildcard characters: False | ||
``` | ||
### -Identity | ||
The identity of the user to remove. This can be the UPN, the GUID or an instance of the user. | ||
```yaml | ||
Type: AzureADUserPipeBind | ||
Parameter Sets: (All) | ||
|
||
Required: True | ||
Position: Named | ||
Default value: None | ||
Accept pipeline input: True (ByValue) | ||
Accept wildcard characters: False | ||
``` | ||
## RELATED LINKS | ||
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using AngleSharp.Io; | ||
using PnP.Framework.Diagnostics; | ||
using PnP.PowerShell.Commands.Attributes; | ||
using PnP.PowerShell.Commands.Base; | ||
using PnP.PowerShell.Commands.Base.PipeBinds; | ||
using PnP.PowerShell.Commands.Utilities; | ||
using System.IO; | ||
using System.Management.Automation; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PnP.PowerShell.Commands.UserProfiles | ||
{ | ||
[Cmdlet(VerbsCommon.Get, "PnPUserProfilePhoto")] | ||
[RequiredApiDelegatedOrApplicationPermissions("graph/ProfilePhoto.ReadWrite.All")] | ||
[RequiredApiDelegatedPermissions("graph/User.ReadWrite")] | ||
[RequiredApiDelegatedOrApplicationPermissions("graph/User.ReadWrite.All")] | ||
public class GetUserProfilePhoto : PnPGraphCmdlet | ||
{ | ||
[Parameter(Mandatory = true, ValueFromPipeline = true)] | ||
public AzureADUserPipeBind Identity; | ||
|
||
[Parameter(Mandatory = false)] | ||
public string Filename; | ||
|
||
[Parameter(Mandatory = false)] | ||
public SwitchParameter Force; | ||
|
||
|
||
protected override void ExecuteCmdlet() | ||
{ | ||
WriteVerbose($"Looking up user provided through the {nameof(Identity)} parameter"); | ||
Model.AzureAD.User user = Identity.GetUser(AccessToken, Connection.AzureEnvironment); | ||
|
||
if (user == null) | ||
{ | ||
Log.Error("Get-PnPUserProfilePhoto", $"User provided through the {nameof(Identity)} parameter could not be found"); | ||
throw new PSArgumentException($"User provided through the {nameof(Identity)} parameter could not be found"); | ||
} | ||
|
||
WriteVerbose($"Setting profile photo for user {user.UserPrincipalName}"); | ||
|
||
if (Filename == null) | ||
{ | ||
// retrieve the metadata first to figure out the file type | ||
var photoData = RequestHelper.Get<PhotoMetadata>($"users/{user.Id}/photo"); | ||
if (photoData != null) | ||
{ | ||
switch (photoData.ContentType) | ||
{ | ||
case "image/jpeg": | ||
{ | ||
Filename = $"{user.DisplayName}.jpg"; | ||
break; | ||
} | ||
case "image/png": | ||
{ | ||
Filename = $"{user.DisplayName}.png"; | ||
break; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
Log.Error("Get-PnPUserProfilePhoto", "Photo not found"); | ||
throw new PSArgumentException("Photo for user not found"); | ||
} | ||
} | ||
|
||
if (!System.IO.Path.IsPathRooted(Filename)) | ||
{ | ||
Filename = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Filename); | ||
} | ||
|
||
var getphoto = true; | ||
if (File.Exists(Filename)) | ||
{ | ||
if (Force || ShouldContinue($"File {Filename} exists. Overwrite?", Properties.Resources.Confirm)) | ||
{ | ||
getphoto = true; | ||
} | ||
else | ||
{ | ||
getphoto = false; | ||
} | ||
} | ||
if (getphoto) | ||
{ | ||
var response = RequestHelper.GetResponse($"users/{user.Id}/photo/$value"); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); | ||
System.IO.File.WriteAllBytes(Filename, content); | ||
WriteObject($"File saved as: {Filename}"); | ||
} | ||
} | ||
} | ||
|
||
internal class PhotoMetadata | ||
{ | ||
[JsonPropertyName("@odata.mediaContentType")] | ||
public string ContentType { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters