Skip to content

Commit 1a1c86d

Browse files
committed
feat: Add mail contact deployment standard script
Add new PowerShell script for deploying mail contacts in CIPP standards. This script provides functionality to manage and deploy mail contact configurations across tenant environments.
1 parent 2c189b2 commit 1a1c86d

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
function Invoke-CIPPStandardDeployMailContact {
2+
<#
3+
.FUNCTIONALITY
4+
Internal
5+
.COMPONENT
6+
(APIName) DeployMailContact
7+
.SYNOPSIS
8+
(Label) Deploy Mail Contact
9+
.DESCRIPTION
10+
(Helptext) Creates a new mail contact in Exchange Online across all selected tenants. The contact will be visible in the Global Address List.
11+
(DocsDescription) This standard creates a new mail contact in Exchange Online. Mail contacts are useful for adding external email addresses to your organization's address book. They can be used for distribution lists, shared mailboxes, and other collaboration scenarios.
12+
.NOTES
13+
CAT
14+
Exchange Standards
15+
TAG
16+
ADDEDCOMPONENT
17+
{"type":"textField","name":"standards.DeployMailContact.ExternalEmailAddress","label":"External Email Address","required":true}
18+
{"type":"textField","name":"standards.DeployMailContact.DisplayName","label":"Display Name","required":true}
19+
{"type":"textField","name":"standards.DeployMailContact.FirstName","label":"First Name","required":false}
20+
{"type":"textField","name":"standards.DeployMailContact.LastName","label":"Last Name","required":false}
21+
IMPACT
22+
Low Impact
23+
ADDEDDATE
24+
2025-05-28
25+
POWERSHELLEQUIVALENT
26+
New-MailContact
27+
RECOMMENDEDBY
28+
"CIPP"
29+
#>
30+
31+
param($Tenant, $Settings)
32+
33+
# Input validation
34+
if (-not $Settings.ExternalEmailAddress -or -not $Settings.DisplayName) {
35+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'DeployMailContact: ExternalEmailAddress and DisplayName are required parameters.' -sev Error
36+
return
37+
}
38+
39+
# Validate email address format
40+
try {
41+
$null = [System.Net.Mail.MailAddress]::new($Settings.ExternalEmailAddress)
42+
}
43+
catch {
44+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "DeployMailContact: Invalid email address format: $($Settings.ExternalEmailAddress)" -sev Error
45+
return
46+
}
47+
48+
# Check if contact already exists
49+
try {
50+
$ExistingContact = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-MailContact' -cmdParams @{
51+
Identity = $Settings.ExternalEmailAddress
52+
ErrorAction = 'Stop'
53+
}
54+
}
55+
catch {
56+
# If the error is that the contact wasn't found, that's expected and we can proceed
57+
if ($_.Exception.Message -like "*couldn't be found*") {
58+
$ExistingContact = $null
59+
}
60+
else {
61+
# For any other error, we should log it and return
62+
$ErrorMessage = Get-CippException -Exception $_
63+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Error checking for existing mail contact: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
64+
return
65+
}
66+
}
67+
68+
if ($ExistingContact) {
69+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mail contact with email $($Settings.ExternalEmailAddress) already exists" -sev Info
70+
return
71+
}
72+
73+
# Remediation
74+
if ($Settings.remediate -eq $true) {
75+
try {
76+
$NewContactParams = @{
77+
ExternalEmailAddress = $Settings.ExternalEmailAddress
78+
DisplayName = $Settings.DisplayName
79+
Name = $Settings.DisplayName
80+
}
81+
82+
# Add optional parameters if provided
83+
if ($Settings.FirstName) { $NewContactParams.FirstName = $Settings.FirstName }
84+
if ($Settings.LastName) { $NewContactParams.LastName = $Settings.LastName }
85+
86+
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'New-MailContact' -cmdParams $NewContactParams
87+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully created mail contact $($Settings.DisplayName) with email $($Settings.ExternalEmailAddress)" -sev Info
88+
}
89+
catch {
90+
$ErrorMessage = Get-CippException -Exception $_
91+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Could not create mail contact. $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
92+
}
93+
}
94+
95+
# Alert
96+
if ($Settings.alert -eq $true) {
97+
if ($ExistingContact) {
98+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mail contact $($Settings.DisplayName) already exists" -sev Info
99+
}
100+
else {
101+
Write-StandardsAlert -message "Mail contact $($Settings.DisplayName) needs to be created" -object @{
102+
DisplayName = $Settings.DisplayName
103+
ExternalEmailAddress = $Settings.ExternalEmailAddress
104+
FirstName = $Settings.FirstName
105+
LastName = $Settings.LastName
106+
} -tenant $Tenant -standardName 'DeployMailContact' -standardId $Settings.standardId
107+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mail contact $($Settings.DisplayName) needs to be created" -sev Info
108+
}
109+
}
110+
111+
# Report
112+
if ($Settings.report -eq $true) {
113+
$ReportData = @{
114+
DisplayName = $Settings.DisplayName
115+
ExternalEmailAddress = $Settings.ExternalEmailAddress
116+
FirstName = $Settings.FirstName
117+
LastName = $Settings.LastName
118+
Exists = [bool]$ExistingContact
119+
}
120+
Add-CIPPBPAField -FieldName 'DeployMailContact' -FieldValue $ReportData -StoreAs json -Tenant $Tenant
121+
122+
if ($ExistingContact) {
123+
$FieldValue = $true
124+
}
125+
else {
126+
$FieldValue = $ReportData
127+
}
128+
Set-CIPPStandardsCompareField -FieldName 'standards.DeployMailContact' -FieldValue $FieldValue -Tenant $Tenant
129+
}
130+
}

0 commit comments

Comments
 (0)