Skip to content

Commit 0766fc1

Browse files
Use Graph lookup and update mail contact (KelvinTegelaar#1929)
Add a Graph lookup for the mail contact (New-GraphGetRequest) to read AD properties (givenName/surname/displayName) instead of relying solely on Exchange objects. Remove the prior prebuilt ContactData, build New-MailContact params inline, and add a remediation branch to call Set-Contact when Graph properties differ from expected settings. Adjust reporting to use the Graph-derived current values and the rebuilt contact data, and keep existing logging and error handling.
2 parents 2da43f2 + d5e686f commit 0766fc1

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,13 @@ function Invoke-CIPPStandardDeployMailContact {
5454
return
5555
}
5656

57-
# Prepare contact data for reuse
58-
$ContactData = @{
59-
DisplayName = $Settings.DisplayName
60-
ExternalEmailAddress = $Settings.ExternalEmailAddress
61-
FirstName = $Settings.FirstName
62-
LastName = $Settings.LastName
63-
}
64-
6557
# Check if contact already exists
6658
try {
6759
$ExistingContact = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-MailContact' -cmdParams @{
6860
Identity = $Settings.ExternalEmailAddress
6961
ErrorAction = 'Stop'
7062
}
63+
$ExistingContactLookup = New-GraphGetRequest -tenantid $Tenant -uri "https://graph.microsoft.com/beta/contacts/$($ExistingContact.ExternalDirectoryObjectId)" -ErrorAction 'Stop'
7164
} catch {
7265
if ($_.Exception.Message -like "*couldn't be found*") {
7366
$ExistingContact = $null
@@ -80,13 +73,33 @@ function Invoke-CIPPStandardDeployMailContact {
8073
# Remediation
8174
if ($Settings.remediate -eq $true -and -not $ExistingContact) {
8275
try {
83-
$NewContactParams = $ContactData.Clone()
76+
$NewContactParams = @{
77+
Name = $Settings.DisplayName
78+
ExternalEmailAddress = $Settings.ExternalEmailAddress
79+
FirstName = $Settings.FirstName
80+
LastName = $Settings.LastName
81+
}
8482
$NewContactParams.Name = $Settings.DisplayName
8583
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'New-MailContact' -cmdParams $NewContactParams
84+
# I would like to update the contact object here but exchange replication delays make it unreliable, so instead it will alert on the discrepancy until the next run - Zac
8685
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully created mail contact $($Settings.DisplayName) with email $($Settings.ExternalEmailAddress)" -sev Info
8786
} catch {
8887
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Could not create mail contact. $(Get-CippException -Exception $_).NormalizedError" -sev Error
8988
}
89+
} elseif ($Settings.remediate -eq $true -and $ExistingContact -and ($ExistingContactLookup.givenName -ne $Settings.FirstName -or $ExistingContactLookup.surname -ne $Settings.LastName -or $ExistingContactLookup.displayName -ne $Settings.DisplayName)) {
90+
try {
91+
$ContactParams = @{
92+
Identity = $ExistingContact.Guid
93+
DisplayName = $Settings.DisplayName
94+
FirstName = $Settings.FirstName
95+
LastName = $Settings.LastName
96+
}
97+
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-Contact' -cmdParams $ContactParams
98+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully updated contact properties for $($Settings.DisplayName)" -sev Info
99+
# I would like to update the contact object here but exchange replication delays make it unreliable, so instead it will alert on the discrepancy until the next run - Zac
100+
} catch {
101+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Could not update mail contact. $(Get-CippException -Exception $_).NormalizedError" -sev Error
102+
}
90103
}
91104

92105
# Alert
@@ -101,21 +114,19 @@ function Invoke-CIPPStandardDeployMailContact {
101114

102115
# Report
103116
if ($Settings.report -eq $true) {
104-
$ReportData = $ContactData.Clone()
105117
$ContactData = @{
106118
DisplayName = $Settings.DisplayName
107119
ExternalEmailAddress = $Settings.ExternalEmailAddress
108120
FirstName = $Settings.FirstName ?? ''
109121
LastName = $Settings.LastName ?? ''
110122
}
111-
$CurrentValue = $ExistingContact | Select-Object DisplayName, ExternalEmailAddress, FirstName, LastName
112123
$currentValue = @{
113-
DisplayName = $ExistingContact.displayName
124+
DisplayName = $ExistingContactLookup.displayName
114125
ExternalEmailAddress = ($ExistingContact.ExternalEmailAddress -replace 'SMTP:', '')
115-
FirstName = $ExistingContact.firstName ?? ''
116-
LastName = $ExistingContact.lastName ?? ''
126+
FirstName = $ExistingContactLookup.givenName ?? ''
127+
LastName = $ExistingContactLookup.surname ?? ''
117128
}
118-
Add-CIPPBPAField -FieldName 'DeployMailContact' -FieldValue $ReportData -StoreAs json -Tenant $Tenant
119-
Set-CIPPStandardsCompareField -FieldName 'standards.DeployMailContact' -CurrentValue $CurrentValue -ExpectedValue $ReportData -Tenant $Tenant
129+
Add-CIPPBPAField -FieldName 'DeployMailContact' -FieldValue $ContactData -StoreAs json -Tenant $Tenant
130+
Set-CIPPStandardsCompareField -FieldName 'standards.DeployMailContact' -CurrentValue $CurrentValue -ExpectedValue $ContactData -Tenant $Tenant
120131
}
121132
}

0 commit comments

Comments
 (0)