Created
July 31, 2020 01:38
-
-
Save gscales/6ba458d3b5ae05e269d9f30457990a28 to your computer and use it in GitHub Desktop.
Get User Photo gist
This file contains hidden or 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
| function Get-GraphUserPhoto { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Position = 0, Mandatory = $true)] | |
| [String] | |
| $Filename, | |
| [Parameter(Position = 1, Mandatory = $true)] | |
| [String] | |
| $MailboxName, | |
| [Parameter(Position = 2, Mandatory = $false)] | |
| [String] | |
| $ClientId, | |
| [Parameter(Position = 3, Mandatory = $false)] | |
| [String] | |
| $RedirectURI = "urn:ietf:wg:oauth:2.0:oob", | |
| [Parameter(Position = 4, Mandatory = $false)] | |
| [String] | |
| $scopes = "User.Read.All Mail.Read", | |
| [Parameter(Position = 5, Mandatory = $false)] | |
| [switch] | |
| $AutoPrompt, | |
| [Parameter(Position = 6, Mandatory = $false)] | |
| [String] | |
| $PhotoSize, | |
| [Parameter(Position = 7, Mandatory = $false)] | |
| [int32] | |
| $ReSizeDimension, | |
| [Parameter(Position = 8, Mandatory = $false)] | |
| [String] | |
| $ReSizeImageForamt="Png" | |
| ) | |
| process { | |
| $prompt = $true | |
| if($AutoPrompt.IsPresent){ | |
| $prompt = $false | |
| } | |
| $EndPoint = "https://graph.microsoft.com/v1.0/users" | |
| $RequestURL = $EndPoint + "('$MailboxName')/photo/`$value" | |
| if(![String]::IsNullOrEmpty($PhotoSize)){ | |
| $RequestURL = $EndPoint + "('$MailboxName')/photos/$PhotoSize/`$value" | |
| } | |
| $AccessToken = Get-AccessTokenForGraph -MailboxName $Mailboxname -ClientId $ClientId -RedirectURI $RedirectURI -scopes $scopes -Prompt:$prompt | |
| $headers = @{ | |
| 'Authorization' = "Bearer $AccessToken" | |
| 'AnchorMailbox' = "$MailboxName" | |
| } | |
| Invoke-WebRequest -Method Get -Uri $RequestURL -UserAgent "GraphBasicsPs101" -Headers $headers -OutFile $Filename | |
| If($ReSizeDimension -gt 0){ | |
| Add-Type -AssemblyName System.Drawing | |
| $OriginalPhotoFile = [System.Drawing.Image]::FromFile((Get-Item $Filename)) | |
| $ResizedPhoto = New-Object System.Drawing.Bitmap($ReSizeDimension,$ReSizeDimension) | |
| $NewGraphic = [System.Drawing.Graphics]::FromImage($ResizedPhoto) | |
| $NewGraphic.CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceCopy; | |
| $NewGraphic.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality; | |
| $NewGraphic.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic; | |
| $NewGraphic.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality; | |
| $NewGraphic.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality; | |
| $NewGraphic.DrawImage($OriginalPhotoFile, 0, 0, $ReSizeDimension,$ReSizeDimension) | |
| $OriginalPhotoFile.Dispose() | |
| $ResizedPhoto.Save($Filename,[System.Drawing.Imaging.ImageFormat]::$ReSizeImageForamt); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment