Skip to content

Commit 89f8e20

Browse files
Merge pull request KelvinTegelaar#1433 from kris6673/fix-standard-profile-photo
Fix: Re-enable Graph functionality for profile photo updates
2 parents 110f073 + cc8e91f commit 89f8e20

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardProfilePhotos.ps1

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,23 @@ function Invoke-CIPPStandardProfilePhotos {
4242
# true if wanted state is enabled, false if disabled
4343
$DesiredState = $StateValue -eq 'enabled'
4444

45-
<#
46-
HACK This does not work, as the API endpoint is not available via GDAP it seems? It works in the Graph Explorer, but not here.
47-
The error is: "Authorization failed because of missing requirement(s)."
48-
I'm keeping the code here for now, so it's much easier to re-enable if Microsoft makes it possible someday. -Bobby
49-
#>
50-
5145
# Get current Graph policy state
52-
# $Uri = 'https://graph.microsoft.com/beta/admin/people/photoUpdateSettings'
53-
# $CurrentGraphState = New-GraphGetRequest -uri $Uri -tenantid $Tenant
54-
# $UsersCanChangePhotos = if (($CurrentGraphState.allowedRoles -contains 'fe930be7-5e62-47db-91af-98c3a49a38b1' -and $CurrentGraphState.allowedRoles -contains '62e90394-69f5-4237-9190-012177145e10') -or
55-
# $null -ne $CurrentGraphState.allowedRoles) { $false } else { $true }
56-
# $GraphStateCorrect = $UsersCanChangePhotos -eq $DesiredState
46+
$Uri = 'https://graph.microsoft.com/beta/admin/people/photoUpdateSettings'
47+
$CurrentGraphState = New-GraphGetRequest -uri $Uri -tenantid $Tenant
48+
$UsersCanChangePhotos = if ([string]::IsNullOrWhiteSpace($CurrentGraphState.allowedRoles) ) { $true } else { $false }
49+
$GraphStateCorrect = $UsersCanChangePhotos -eq $DesiredState
5750

51+
if ($UsersCanChangePhotos -eq $false -and $DesiredState -eq $false) {
52+
# Check if the correct roles are present
53+
$GraphStateCorrect = $CurrentGraphState.allowedRoles -contains '62e90394-69f5-4237-9190-012177145e10' -and $CurrentGraphState.allowedRoles -contains 'fe930be7-5e62-47db-91af-98c3a49a38b1'
54+
}
5855

5956
# Get current OWA mailbox policy state
6057
$CurrentOWAState = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OwaMailboxPolicy' -cmdParams @{Identity = 'OwaMailboxPolicy-Default' } -Select 'Identity,SetPhotoEnabled'
6158
$OWAStateCorrect = $CurrentOWAState.SetPhotoEnabled -eq $DesiredState
62-
# $CurrentStatesCorrect = $GraphStateCorrect -eq $true -and $OWAStateCorrect -eq $true
63-
$CurrentStatesCorrect = $OWAStateCorrect -eq $true
59+
60+
# Check if both states are correct
61+
$CurrentStatesCorrect = $GraphStateCorrect -eq $true -and $OWAStateCorrect -eq $true
6462

6563
if ($Settings.remediate -eq $true) {
6664
Write-Host 'Time to remediate'
@@ -72,23 +70,23 @@ function Invoke-CIPPStandardProfilePhotos {
7270
Write-Host 'Enabling'
7371
# Enable photo updates
7472
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-OwaMailboxPolicy' -cmdParams @{Identity = $CurrentOWAState.Identity; SetPhotoEnabled = $true } -useSystemMailbox $true
75-
# $null = New-GraphRequest -uri $Uri -tenant $Tenant -type DELETE
73+
$null = New-GraphPostRequest -uri $Uri -tenant $Tenant -type DELETE -AsApp $true
7674
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Set Profile photo settings to $StateValue" -sev Info
7775

7876
} else {
7977
Write-Host 'Disabling'
8078
# Disable photo updates
8179
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-OwaMailboxPolicy' -cmdParams @{Identity = $CurrentOWAState.Identity; SetPhotoEnabled = $false } -useSystemMailbox $true
8280

83-
# $body = @{
84-
# source = 'cloud'
85-
# allowedRoles = @(
86-
# 'fe930be7-5e62-47db-91af-98c3a49a38b1', # Global admin
87-
# '62e90394-69f5-4237-9190-012177145e10' # User admin
88-
# )
89-
# }
90-
# $body = ConvertTo-Json -InputObject $body -Depth 5 -Compress
91-
# $null = New-GraphPostRequest -uri $Uri -tenant $Tenant -body $body -type PATCH -AsApp $true
81+
$body = @{
82+
source = 'cloud'
83+
allowedRoles = @(
84+
'fe930be7-5e62-47db-91af-98c3a49a38b1', # Global admin
85+
'62e90394-69f5-4237-9190-012177145e10' # User admin
86+
)
87+
}
88+
$body = ConvertTo-Json -InputObject $body -Depth 5 -Compress
89+
$null = New-GraphPostRequest -uri $Uri -tenant $Tenant -body $body -type PATCH -AsApp $true
9290
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Set Profile photo settings to $StateValue" -sev Info
9391
}
9492
} catch {
@@ -115,7 +113,10 @@ function Invoke-CIPPStandardProfilePhotos {
115113
if ($CurrentStatesCorrect) {
116114
$FieldValue = $true
117115
} else {
118-
$FieldValue = $CurrentOWAState
116+
$FieldValue = [PSCustomObject]@{
117+
OwaStateCorrect = $OWAStateCorrect
118+
GraphStateCorrect = $GraphStateCorrect
119+
}
119120
}
120121
Set-CIPPStandardsCompareField -FieldName 'standards.ProfilePhotos' -FieldValue $FieldValue -Tenant $Tenant
121122
}

0 commit comments

Comments
 (0)