Skip to content

Commit 800d924

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. Tweak to prevent early code exit Further adjustment to code execution Streamlined
1 parent 579e129 commit 800d924

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 ([string]::IsNullOrWhiteSpace($Settings.DisplayName)) {
35+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'DeployMailContact: DisplayName cannot be empty or just whitespace.' -sev Error
36+
return
37+
}
38+
39+
try {
40+
$null = [System.Net.Mail.MailAddress]::new($Settings.ExternalEmailAddress)
41+
}
42+
catch {
43+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "DeployMailContact: Invalid email address format: $($Settings.ExternalEmailAddress)" -sev Error
44+
return
45+
}
46+
47+
# Prepare contact data for reuse
48+
$ContactData = @{
49+
DisplayName = $Settings.DisplayName
50+
ExternalEmailAddress = $Settings.ExternalEmailAddress
51+
FirstName = $Settings.FirstName
52+
LastName = $Settings.LastName
53+
}
54+
55+
# Check if contact already exists
56+
try {
57+
$ExistingContact = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-MailContact' -cmdParams @{
58+
Identity = $Settings.ExternalEmailAddress
59+
ErrorAction = 'Stop'
60+
}
61+
}
62+
catch {
63+
if ($_.Exception.Message -like "*couldn't be found*") {
64+
$ExistingContact = $null
65+
}
66+
else {
67+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Error checking for existing mail contact: $(Get-CippException -Exception $_).NormalizedError" -sev Error
68+
return
69+
}
70+
}
71+
72+
# Remediation
73+
if ($Settings.remediate -eq $true -and -not $ExistingContact) {
74+
try {
75+
$NewContactParams = $ContactData.Clone()
76+
$NewContactParams.Name = $Settings.DisplayName
77+
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'New-MailContact' -cmdParams $NewContactParams
78+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully created mail contact $($Settings.DisplayName) with email $($Settings.ExternalEmailAddress)" -sev Info
79+
}
80+
catch {
81+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Could not create mail contact. $(Get-CippException -Exception $_).NormalizedError" -sev Error
82+
}
83+
}
84+
85+
# Alert
86+
if ($Settings.alert -eq $true) {
87+
if ($ExistingContact) {
88+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mail contact $($Settings.DisplayName) already exists" -sev Info
89+
}
90+
else {
91+
Write-StandardsAlert -message "Mail contact $($Settings.DisplayName) needs to be created" -object $ContactData -tenant $Tenant -standardName 'DeployMailContact' -standardId $Settings.standardId
92+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mail contact $($Settings.DisplayName) needs to be created" -sev Info
93+
}
94+
}
95+
96+
# Report
97+
if ($Settings.report -eq $true) {
98+
$ReportData = $ContactData.Clone()
99+
$ReportData.Exists = [bool]$ExistingContact
100+
Add-CIPPBPAField -FieldName 'DeployMailContact' -FieldValue $ReportData -StoreAs json -Tenant $Tenant
101+
Set-CIPPStandardsCompareField -FieldName 'standards.DeployMailContact' -FieldValue $($ExistingContact ? $true : $ReportData) -Tenant $Tenant
102+
}
103+
}

0 commit comments

Comments
 (0)