Skip to content

Commit d585ae2

Browse files
committed
custom data mapping
1 parent c0eae0f commit d585ae2

File tree

7 files changed

+129
-18
lines changed

7 files changed

+129
-18
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function Invoke-CustomDataSync {
2+
param(
3+
$TenantFilter
4+
)
5+
6+
$Table = Get-CIPPTable -TableName CustomDataMapping
7+
$CustomData = Get-CIPPAzDataTableEntity @Table
8+
9+
$Mappings = $CustomData | ForEach-Object {
10+
$Mapping = $_.JSON | ConvertFrom-Json
11+
$Mapping
12+
}
13+
14+
Write-Host ($Mappings | ConvertTo-Json -Depth 10)
15+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecCustomData.ps1

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Invoke-ExecCustomData {
1010

1111
$Action = $Request.Query.Action ?? $Request.Body.Action
1212
$CustomDataTable = Get-CippTable -TableName 'CustomData'
13+
$CustomDataMappingsTable = Get-CippTable -TableName 'CustomDataMappings'
1314

1415
Write-Information "Executing action '$Action'"
1516

@@ -231,6 +232,19 @@ function Invoke-ExecCustomData {
231232
try {
232233
$Uri = "https://graph.microsoft.com/beta/applications(appId='$($env:ApplicationId)')/extensionProperties"
233234
$DirectoryExtensions = New-GraphGetRequest -uri $Uri -AsApp $true -NoAuthCheck $true -tenantid $env:TenantID
235+
$Existing = Get-CIPPAzDataTableEntity @CustomDataTable -Filter "PartitionKey eq 'DirectoryExtension'"
236+
237+
foreach ($DirectoryExtension in $DirectoryExtensions) {
238+
if ($Existing -match $DirectoryExtension.name) {
239+
continue
240+
}
241+
$Entity = @{
242+
PartitionKey = 'DirectoryExtension'
243+
RowKey = $DirectoryExtension.name
244+
JSON = [string](ConvertTo-Json $DirectoryExtension -Compress -Depth 5)
245+
}
246+
Add-CIPPAzDataTableEntity @CustomDataTable -Entity $Entity -Force
247+
}
234248

235249
$Body = @{
236250
Results = @($DirectoryExtensions)
@@ -281,8 +295,9 @@ function Invoke-ExecCustomData {
281295
$Entity = @{
282296
PartitionKey = 'DirectoryExtension'
283297
RowKey = $Response.name
284-
JSON = [string](ConvertFrom-Json $Response -Compress -Depth 5)
298+
JSON = [string](ConvertTo-Json $Response -Compress -Depth 5)
285299
}
300+
Add-CIPPAzDataTableEntity @CustomDataTable -Entity $Entity -Force
286301
} catch {
287302
$Body = @{
288303
Results = @(
@@ -341,6 +356,64 @@ function Invoke-ExecCustomData {
341356
Results = @($AvailableAttributes)
342357
}
343358
}
359+
'ListMappings' {
360+
try {
361+
$Mappings = Get-CIPPAzDataTableEntity @CustomDataMappingsTable | ForEach-Object {
362+
$Mapping = $_.JSON | ConvertFrom-Json -AsHashtable
363+
[PSCustomObject]@{
364+
id = $_.RowKey
365+
tenant = $Mapping.tenantFilter.label
366+
sourceDataset = $Mapping.sourceDataset.label
367+
}
368+
}
369+
$Body = @{
370+
Results = @($Mappings)
371+
}
372+
} catch {
373+
$Body = @{
374+
Results = @(
375+
@{
376+
state = 'error'
377+
resultText = "Failed to retrieve mappings: $($_.Exception.Message)"
378+
}
379+
)
380+
}
381+
}
382+
}
383+
'AddMapping' {
384+
try {
385+
$Mapping = $Request.Body.Mapping
386+
if (!$Mapping) {
387+
throw 'Mapping data is missing in the request body.'
388+
}
389+
$id = [Guid]::NewGuid().ToString()
390+
$Entity = @{
391+
PartitionKey = 'Mapping'
392+
RowKey = $id
393+
JSON = [string]($Mapping | ConvertTo-Json -Depth 5 -Compress)
394+
}
395+
396+
Add-CIPPAzDataTableEntity @CustomDataMappingsTable -Entity $Entity -Force
397+
Register-CIPPExtensionScheduledTasks
398+
399+
$Body = @{
400+
Results = @{
401+
state = 'success'
402+
resultText = "Mapping with ID '$($id)' added successfully."
403+
}
404+
}
405+
} catch {
406+
$Body = @{
407+
Results = @(
408+
@{
409+
state = 'error'
410+
resultText = "Failed to add mapping: $($_.Exception.Message)"
411+
}
412+
)
413+
}
414+
}
415+
}
416+
344417
default {
345418
$Body = @{
346419
Results = @(

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ExecRemoveMailboxRule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Function Invoke-ExecRemoveMailboxRule {
1515
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message 'Accessed this API' -Sev 'Debug'
1616

1717
# Interact with the query or body of the request
18-
$TenantFilter = $Request.Query.TenantFilter ?? $Request.Query.TenantFilter
18+
$TenantFilter = $Request.Query.TenantFilter ?? $Request.Body.TenantFilter
1919
$RuleName = $Request.Query.ruleName ?? $Request.Body.ruleName
2020
$RuleId = $Request.Query.ruleId ?? $Request.Body.ruleId
2121
$Username = $Request.Query.userPrincipalName ?? $Request.Body.userPrincipalName

Modules/CIPPCore/Public/Get-CippCustomDataAttributes.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ function Get-CippCustomDataAttributes {
1818
if ($Type -eq 'SchemaExtension') {
1919
$Name = $CustomData.id
2020
foreach ($TargetObject in $CustomData.targetTypes) {
21-
[PSCustomObject]@{
22-
name = $Name
23-
type = $Type
24-
targetObject = $TargetObject.ToLower()
25-
properties = $CustomData.properties
21+
foreach ($Property in $CustomData.properties) {
22+
[PSCustomObject]@{
23+
name = '{0}.{1}' -f $Name, $Property.name
24+
type = $Type
25+
targetObject = $TargetObject
26+
dataType = $Property.type
27+
isMultiValued = $false
28+
}
2629
}
2730
}
2831
} elseif ($Type -eq 'DirectoryExtension') {
29-
$Name = $CustomData.RowKey
32+
$Name = $CustomDataEntity.RowKey
3033
foreach ($TargetObject in $CustomData.targetObjects) {
3134
[PSCustomObject]@{
3235
name = $Name

Modules/CippExtensions/Public/Extension Functions/Push-CippExtensionData.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ function Push-CippExtensionData {
1414
Invoke-HuduExtensionSync -Configuration $Config -TenantFilter $TenantFilter
1515
}
1616
}
17+
'CustomData' {
18+
Write-Host 'Perfoming Custom Data Extension Sync...'
19+
Invoke-CustomDataSync -TenantFilter $TenantFilter
20+
}
1721
}
1822
}

Modules/CippExtensions/Public/Extension Functions/Register-CippExtensionScheduledTasks.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function Register-CIPPExtensionScheduledTasks {
2-
Param(
2+
param(
33
[switch]$Reschedule
44
)
55

@@ -14,13 +14,23 @@ function Register-CIPPExtensionScheduledTasks {
1414
$PushTasks = Get-CIPPAzDataTableEntity @ScheduledTasksTable -Filter 'Hidden eq true' | Where-Object { $_.Command -match 'Push-CippExtensionData' }
1515
$Tenants = Get-Tenants -IncludeErrors
1616

17-
$Extensions = @('Hudu', 'NinjaOne')
17+
$Extensions = @('Hudu', 'NinjaOne', 'CustomData')
1818
$MappedTenants = [System.Collections.Generic.List[string]]::new()
1919
foreach ($Extension in $Extensions) {
2020
$ExtensionConfig = $Config.$Extension
21-
if ($ExtensionConfig.Enabled -eq $true) {
22-
$Mappings = Get-CIPPAzDataTableEntity @MappingsTable -Filter "PartitionKey eq '$($Extension)Mapping'"
23-
$FieldMapping = Get-CIPPAzDataTableEntity @MappingsTable -Filter "PartitionKey eq '$($Extension)FieldMapping'"
21+
if ($ExtensionConfig.Enabled -eq $true -or $Extension -eq 'CustomData') {
22+
if ($Extension -eq 'CustomData') {
23+
$CustomDataMappingTable = Get-CIPPTable -TableName CustomDataMapping
24+
$Mappings = Get-CIPPAzDataTableEntity @CustomDataMappingTable | ForEach-Object {
25+
$Mapping = $_.JSON | ConvertFrom-Json
26+
[pscustomobject]@{
27+
RowKey = $Mapping.tenantFilter.value
28+
}
29+
}
30+
} else {
31+
$Mappings = Get-CIPPAzDataTableEntity @MappingsTable -Filter "PartitionKey eq '$($Extension)Mapping'"
32+
$FieldMapping = Get-CIPPAzDataTableEntity @MappingsTable -Filter "PartitionKey eq '$($Extension)FieldMapping'"
33+
}
2434
$FieldSync = @{}
2535
$SyncTypes = [System.Collections.Generic.List[string]]::new()
2636

@@ -35,7 +45,7 @@ function Register-CIPPExtensionScheduledTasks {
3545
$SyncTypes.Add('Devices')
3646

3747
foreach ($Mapping in $Mappings) {
38-
$Tenant = $Tenants | Where-Object { $_.customerId -eq $Mapping.RowKey }
48+
$Tenant = $Tenants | Where-Object { $_.customerId -eq $Mapping.RowKey -or $_.defaultDomainName -eq $Mapping.RowKey -or $Mapping.RowKey -eq 'AllTenants' }
3949
if (!$Tenant) {
4050
Write-Warning "Tenant $($Mapping.RowKey) not found"
4151
continue

Modules/CippExtensions/Public/Extension Functions/Sync-CippExtensionData.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,26 @@ function Sync-CippExtensionData {
151151
)
152152
}
153153
'Mailboxes' {
154-
$Select = 'id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ProhibitSendQuota,ProhibitSendReceiveQuota,LitigationHoldEnabled,InPlaceHolds,HiddenFromAddressListsEnabled'
154+
$Select = 'id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ForwardingSmtpAddress,DeliverToMailboxAndForward,ForwardingAddress,HiddenFromAddressListsEnabled,ExternalDirectoryObjectId,MessageCopyForSendOnBehalfEnabled,MessageCopyForSentAsEnabled'
155155
$ExoRequest = @{
156156
tenantid = $TenantFilter
157157
cmdlet = 'Get-Mailbox'
158158
cmdParams = @{}
159159
Select = $Select
160160
}
161-
$Mailboxes = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ArchiveGuid, WhenSoftDeleted, ProhibitSendQuota, ProhibitSendReceiveQuota, LitigationHoldEnabled, InplaceHolds, HiddenFromAddressListsEnabled, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } },
162-
161+
$Mailboxes = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ArchiveGuid, WhenSoftDeleted, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } },
163162
@{ Name = 'displayName'; Expression = { $_.'DisplayName' } },
164163
@{ Name = 'primarySmtpAddress'; Expression = { $_.'PrimarySMTPAddress' } },
165164
@{ Name = 'recipientType'; Expression = { $_.'RecipientType' } },
166165
@{ Name = 'recipientTypeDetails'; Expression = { $_.'RecipientTypeDetails' } },
167-
@{ Name = 'AdditionalEmailAddresses'; Expression = { ($_.'EmailAddresses' | Where-Object { $_ -clike 'smtp:*' }).Replace('smtp:', '') -join ', ' } }
166+
@{ Name = 'AdditionalEmailAddresses'; Expression = { ($_.'EmailAddresses' | Where-Object { $_ -clike 'smtp:*' }).Replace('smtp:', '') -join ', ' } },
167+
@{Name = 'ForwardingSmtpAddress'; Expression = { $_.'ForwardingSmtpAddress' -replace 'smtp:', '' } },
168+
@{Name = 'InternalForwardingAddress'; Expression = { $_.'ForwardingAddress' } },
169+
DeliverToMailboxAndForward,
170+
HiddenFromAddressListsEnabled,
171+
ExternalDirectoryObjectId,
172+
MessageCopyForSendOnBehalfEnabled,
173+
MessageCopyForSentAsEnabled
168174

169175
$Entity = @{
170176
PartitionKey = $TenantFilter

0 commit comments

Comments
 (0)