Skip to content

Commit

Permalink
Merge pull request #4103 from raandree/feature/EXOMailContactExtension
Browse files Browse the repository at this point in the history
EXOMailContact: Added support for Custom Attributes and Extension Custom Attributes
  • Loading branch information
NikCharlebois authored Jan 10, 2024
2 parents 4814fe7 + 273c1d2 commit ac7c75d
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,86 @@ function Get-TargetResource
[System.Boolean]
$UsePreferMessageFormat,

[Parameter()]
[System.String]
$CustomAttribute1,

[Parameter()]
[System.String]
$CustomAttribute2,

[Parameter()]
[System.String]
$CustomAttribute3,

[Parameter()]
[System.String]
$CustomAttribute4,

[Parameter()]
[System.String]
$CustomAttribute5,

[Parameter()]
[System.String]
$CustomAttribute6,

[Parameter()]
[System.String]
$CustomAttribute7,

[Parameter()]
[System.String]
$CustomAttribute8,

[Parameter()]
[System.String]
$CustomAttribute9,

[Parameter()]
[System.String]
$CustomAttribute10,

[Parameter()]
[System.String]
$CustomAttribute11,

[Parameter()]
[System.String]
$CustomAttribute12,

[Parameter()]
[System.String]
$CustomAttribute13,

[Parameter()]
[System.String]
$CustomAttribute14,

[Parameter()]
[System.String]
$CustomAttribute15,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute1,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute2,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute3,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute4,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute5,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
Expand Down Expand Up @@ -135,7 +215,7 @@ function Get-TargetResource

try
{
$contact = Get-MailContact -Identity $Name -ErrorAction Continue
$contact = Get-MailContact -Identity $Name -ErrorAction SilentlyContinue

if ($null -eq $contact)
{
Expand Down Expand Up @@ -170,6 +250,21 @@ function Get-TargetResource
TenantId = $TenantId
}

foreach ($i in (1..15))
{
if ($contact."CustomAttribute$i")
{
$result."CustomAttribute$i" = $contact."CustomAttribute$i"
}
}
foreach ($i in (1..5))
{
if ($contact."ExtensionCustomAttribute$i")
{
$result."ExtensionCustomAttribute$i" = $contact."ExtensionCustomAttribute$i"
}
}

Write-Verbose -Message "Found Mail Contact $($Name)"
Write-Verbose -Message "Get-TargetResource Result: `n $(Convert-M365DscHashtableToString -Hashtable $result)"
return $result
Expand Down Expand Up @@ -258,6 +353,86 @@ function Set-TargetResource
[System.Boolean]
$UsePreferMessageFormat,

[Parameter()]
[System.String]
$CustomAttribute1,

[Parameter()]
[System.String]
$CustomAttribute2,

[Parameter()]
[System.String]
$CustomAttribute3,

[Parameter()]
[System.String]
$CustomAttribute4,

[Parameter()]
[System.String]
$CustomAttribute5,

[Parameter()]
[System.String]
$CustomAttribute6,

[Parameter()]
[System.String]
$CustomAttribute7,

[Parameter()]
[System.String]
$CustomAttribute8,

[Parameter()]
[System.String]
$CustomAttribute9,

[Parameter()]
[System.String]
$CustomAttribute10,

[Parameter()]
[System.String]
$CustomAttribute11,

[Parameter()]
[System.String]
$CustomAttribute12,

[Parameter()]
[System.String]
$CustomAttribute13,

[Parameter()]
[System.String]
$CustomAttribute14,

[Parameter()]
[System.String]
$CustomAttribute15,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute1,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute2,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute3,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute4,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute5,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
Expand Down Expand Up @@ -320,21 +495,24 @@ function Set-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

$setParameters = $PSBoundParameters
$setParameters.Remove('Credential') | Out-Null
$setParameters.Remove('ApplicationId') | Out-Null
$setParameters.Remove('TenantId') | Out-Null
$setParameters.Remove('CertificateThumbprint') | Out-Null
$setParameters.Remove('CertificatePath') | Out-Null
$setParameters.Remove('CertificatePassword') | Out-Null
$setParameters.Remove('ManagedIdentity') | Out-Null
$setParameters.Remove('Ensure') | Out-Null

# Mail Contact doesn't exist but it should
if ($Ensure -eq 'Present' -and $currentContact.Ensure -eq 'Absent')
{
$parameters = Sync-M365DSCParameter -Command (Get-Command -Name New-MailContact) -Parameters $PSBoundParameters
Write-Verbose -Message "The Mail Contact '$($Name)' does not exist but it should. Creating Mail Contact."
New-MailContact @setParameters

try
{
New-MailContact @parameters -ErrorAction Stop

$parameters = Sync-M365DSCParameter -Command (Get-Command -Name Set-MailContact) -Parameters $PSBoundParameters
$parameters.Identity = $Name
Set-MailContact @parameters -ErrorAction Stop
}
catch
{
Write-Error -ErrorRecord $_
}
}
# Mail Contact exists but shouldn't
elseif ($Ensure -eq 'Absent' -and $currentContact.Ensure -eq 'Present')
Expand All @@ -345,10 +523,10 @@ function Set-TargetResource
elseif ($Ensure -eq 'Present' -and $currentContact.Ensure -eq 'Present')
{
Write-Verbose -Message "Mail Contact '$($Name)' already exists. Updating settings"
Write-Verbose -Message "Updating Mail Contact '$($Name)' with values: $(Convert-M365DscHashtableToString -Hashtable $setParameters)"
$setParameters.Add('Identity', $Name)
$setParameters.Remove('OrganizationalUnit') | Out-Null
Set-MailContact @setParameters
$parameters = Sync-M365DSCParameter -Command (Get-Command -Name Set-MailContact) -Parameters $PSBoundParameters
Write-Verbose -Message "Updating Mail Contact '$($Name)' with values: $(Convert-M365DscHashtableToString -Hashtable $parameters)"
$parameters.Identity = $Name
Set-MailContact @parameters
}
}

Expand Down Expand Up @@ -424,6 +602,86 @@ function Test-TargetResource
[System.Boolean]
$UsePreferMessageFormat,

[Parameter()]
[System.String]
$CustomAttribute1,

[Parameter()]
[System.String]
$CustomAttribute2,

[Parameter()]
[System.String]
$CustomAttribute3,

[Parameter()]
[System.String]
$CustomAttribute4,

[Parameter()]
[System.String]
$CustomAttribute5,

[Parameter()]
[System.String]
$CustomAttribute6,

[Parameter()]
[System.String]
$CustomAttribute7,

[Parameter()]
[System.String]
$CustomAttribute8,

[Parameter()]
[System.String]
$CustomAttribute9,

[Parameter()]
[System.String]
$CustomAttribute10,

[Parameter()]
[System.String]
$CustomAttribute11,

[Parameter()]
[System.String]
$CustomAttribute12,

[Parameter()]
[System.String]
$CustomAttribute13,

[Parameter()]
[System.String]
$CustomAttribute14,

[Parameter()]
[System.String]
$CustomAttribute15,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute1,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute2,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute3,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute4,

[Parameter()]
[System.String[]]
$ExtensionCustomAttribute5,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
Expand Down Expand Up @@ -477,15 +735,14 @@ function Test-TargetResource
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $PSBoundParameters)"

$ValuesToCheck = $PSBoundParameters
$ValuesToCheck.Remove('Credential') | Out-Null
$ValuesToCheck.Remove('ApplicationId') | Out-Null
$ValuesToCheck.Remove('TenantId') | Out-Null
$ValuesToCheck.Remove('CertificateThumbprint') | Out-Null
$ValuesToCheck.Remove('CertificatePath') | Out-Null
$ValuesToCheck.Remove('CertificatePassword') | Out-Null
$ValuesToCheck.Remove('ManagedIdentity') | Out-Null

$ValuesToCheck.Remove('OrganizationalUnit') | Out-Null
[void]$ValuesToCheck.Remove('Credential')
[void]$ValuesToCheck.Remove('ApplicationId')
[void]$ValuesToCheck.Remove('TenantId')
[void]$ValuesToCheck.Remove('CertificateThumbprint')
[void]$ValuesToCheck.Remove('CertificatePath')
[void]$ValuesToCheck.Remove('CertificatePassword')
[void]$ValuesToCheck.Remove('ManagedIdentity')
[void]$ValuesToCheck.Remove('OrganizationalUnit')

$TestResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
Expand Down Expand Up @@ -581,7 +838,7 @@ function Export-TargetResource
-ModulePath $PSScriptRoot `
-Results $Results `
-Credential $Credential
$dscContent.Append($currentDSCBlock) | Out-Null
[void]$dscContent.Append($currentDSCBlock)

Save-M365DSCPartialExport -Content $currentDSCBlock `
-FileName $Global:PartialExportFileName
Expand Down
Loading

0 comments on commit ac7c75d

Please sign in to comment.