@@ -31,12 +31,11 @@ function Invoke-CIPPStandardDeployMailContact {
3131 param ($Tenant , $Settings )
3232
3333 # 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
34+ if ([ string ]::IsNullOrWhiteSpace( $Settings.DisplayName ) ) {
35+ Write-LogMessage - API ' Standards' - tenant $Tenant - message ' DeployMailContact: DisplayName cannot be empty or just whitespace .' - sev Error
3636 return
3737 }
3838
39- # Validate email address format
4039 try {
4140 $null = [System.Net.Mail.MailAddress ]::new($Settings.ExternalEmailAddress )
4241 }
@@ -45,6 +44,14 @@ function Invoke-CIPPStandardDeployMailContact {
4544 return
4645 }
4746
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+
4855 # Check if contact already exists
4956 try {
5057 $ExistingContact = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-MailContact' - cmdParams @ {
@@ -53,42 +60,25 @@ function Invoke-CIPPStandardDeployMailContact {
5360 }
5461 }
5562 catch {
56- # If the error is that the contact wasn't found, that's expected and we can proceed
5763 if ($_.Exception.Message -like " *couldn't be found*" ) {
5864 $ExistingContact = $null
5965 }
6066 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
67+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Error checking for existing mail contact: $ ( Get-CippException - Exception $_ ) .NormalizedError" - sev Error
6468 return
6569 }
6670 }
6771
6872 # Remediation
69- if ($Settings.remediate -eq $true ) {
70- if ($ExistingContact ) {
71- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Mail contact with email $ ( $Settings.ExternalEmailAddress ) already exists" - sev Info
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
7279 }
73- else {
74- try {
75- $NewContactParams = @ {
76- ExternalEmailAddress = $Settings.ExternalEmailAddress
77- DisplayName = $Settings.DisplayName
78- Name = $Settings.DisplayName
79- }
80-
81- # Add optional parameters if provided
82- if ($Settings.FirstName ) { $NewContactParams.FirstName = $Settings.FirstName }
83- if ($Settings.LastName ) { $NewContactParams.LastName = $Settings.LastName }
84-
85- $null = New-ExoRequest - tenantid $Tenant - cmdlet ' New-MailContact' - cmdParams $NewContactParams
86- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Successfully created mail contact $ ( $Settings.DisplayName ) with email $ ( $Settings.ExternalEmailAddress ) " - sev Info
87- }
88- catch {
89- $ErrorMessage = Get-CippException - Exception $_
90- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Could not create mail contact. $ ( $ErrorMessage.NormalizedError ) " - sev Error - LogData $ErrorMessage
91- }
80+ catch {
81+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Could not create mail contact. $ ( Get-CippException - Exception $_ ) .NormalizedError" - sev Error
9282 }
9383 }
9484
@@ -98,33 +88,16 @@ function Invoke-CIPPStandardDeployMailContact {
9888 Write-LogMessage - API ' Standards' - tenant $Tenant - message " Mail contact $ ( $Settings.DisplayName ) already exists" - sev Info
9989 }
10090 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
91+ Write-StandardsAlert - message " Mail contact $ ( $Settings.DisplayName ) needs to be created" - object $ContactData - tenant $Tenant - standardName ' DeployMailContact' - standardId $Settings.standardId
10792 Write-LogMessage - API ' Standards' - tenant $Tenant - message " Mail contact $ ( $Settings.DisplayName ) needs to be created" - sev Info
10893 }
10994 }
11095
11196 # Report
11297 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- }
98+ $ReportData = $ContactData.Clone ()
99+ $ReportData.Exists = [bool ]$ExistingContact
120100 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
101+ Set-CIPPStandardsCompareField - FieldName ' standards.DeployMailContact' - FieldValue $ ($ExistingContact ? $true : $ReportData ) - Tenant $Tenant
129102 }
130103}
0 commit comments