Skip to content

Commit

Permalink
Merge pull request #954 from JohnDuprey/dev
Browse files Browse the repository at this point in the history
Extension sync - add mailbox permission/usage/cas
  • Loading branch information
JohnDuprey authored Jul 9, 2024
2 parents 624fdd4 + 10c62de commit 8ec917e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 54 deletions.
8 changes: 4 additions & 4 deletions Modules/CIPPCore/Public/Add-CIPPAzDataTableEntity.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Add-CIPPAzDataTableEntity {
try {
Add-AzDataTableEntity -Context $Context -Force:$Force -CreateTableIfNotExists:$CreateTableIfNotExists -Entity $SingleEnt -ErrorAction Stop
} catch [System.Exception] {
if ($_.Exception.ErrorCode -eq 'PropertyValueTooLarge' -or $_.Exception.ErrorCode -eq 'EntityTooLarge') {
if ($_.Exception.ErrorCode -eq 'PropertyValueTooLarge' -or $_.Exception.ErrorCode -eq 'EntityTooLarge' -or $_.Exception.ErrorCode -eq 'RequestBodyTooLarge') {
try {
$largePropertyNames = [System.Collections.ArrayList]::new()
$entitySize = 0
Expand Down Expand Up @@ -67,7 +67,7 @@ function Add-CIPPAzDataTableEntity {
$entityIndex = 0

while ($entitySize -gt $MaxRowSize) {
Write-Host "Entity size is $entitySize. Splitting entity into multiple parts."
Write-Information "Entity size is $entitySize. Splitting entity into multiple parts."
$newEntity = @{}
$newEntity['PartitionKey'] = $originalPartitionKey
if ($entityIndex -eq 0) {
Expand Down Expand Up @@ -126,7 +126,7 @@ function Add-CIPPAzDataTableEntity {
}

foreach ($row in $rows) {
Write-Host "current entity is $($row.RowKey) with $($row.PartitionKey). Our size is $([System.Text.Encoding]::UTF8.GetByteCount($($row | ConvertTo-Json)))"
Write-Information "current entity is $($row.RowKey) with $($row.PartitionKey). Our size is $([System.Text.Encoding]::UTF8.GetByteCount($($row | ConvertTo-Json)))"
Add-AzDataTableEntity -Context $Context -Force:$Force -CreateTableIfNotExists:$CreateTableIfNotExists -Entity $row
}
} else {
Expand All @@ -137,7 +137,7 @@ function Add-CIPPAzDataTableEntity {
throw "Error processing entity: $($_.Exception.Message) Linenumner: $($_.InvocationInfo.ScriptLineNumber)"
}
} else {
Write-Host "THE ERROR IS $($_.Exception.ErrorCode). The size of the entity is $entitySize."
Write-Information "THE ERROR IS $($_.Exception.ErrorCode). The size of the entity is $entitySize."
throw $_
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Register-CIPPExtensionScheduledTasks {
$Tenants = Get-Tenants -IncludeErrors

$Extensions = @('Hudu')

$MappedTenants = [System.Collections.Generic.List[string]]::new()
foreach ($Extension in $Extensions) {
$ExtensionConfig = $Config.$Extension
if ($ExtensionConfig.Enabled -eq $true) {
Expand Down Expand Up @@ -45,6 +45,7 @@ function Register-CIPPExtensionScheduledTasks {
Write-Warning "Tenant $($Mapping.RowKey) not found"
continue
}
$MappedTenants.Add($Tenant.defaultDomainName)
foreach ($SyncType in $SyncTypes) {
$ExistingTask = $ScheduledTasks | Where-Object { $_.Tenant -eq $Tenant.defaultDomainName -and $_.SyncType -eq $SyncType }
if (!$ExistingTask -or $Reschedule.IsPresent) {
Expand Down Expand Up @@ -98,23 +99,24 @@ function Register-CIPPExtensionScheduledTasks {
}
} else {
# remove existing scheduled tasks
$ScheduledTasks | Where-Object { $_.SyncType -eq $Extension } | ForEach-Object {
$PushTasks | Where-Object { $_.SyncType -eq $Extension } | ForEach-Object {
Write-Information "Extension Disabled: Cleaning up scheduled task $($_.Name) for tenant $($_.Tenant)"
$Entity = $_ | Select-Object -Property PartitionKey, RowKey
Remove-AzDataTableEntity @ScheduledTasksTable -Entity $Entity
}
}
}
$MappedTenants = $MappedTenants | Sort-Object -Unique

foreach ($Task in $ScheduledTasks) {
if ($Task.Tenant -notin $Tenants.defaultDomainName) {
if ($Task.Tenant -notin $MappedTenants) {
Write-Information "Tenant Removed: Cleaning up scheduled task $($Task.Name) for tenant $($Task.TenantFilter)"
$Entity = $Task | Select-Object -Property PartitionKey, RowKey
Remove-AzDataTableEntity @ScheduledTasksTable -Entity $Entity
}
}
foreach ($Task in $PushTasks) {
if ($Task.Tenant -notin $Tenants.defaultDomainName) {
if ($Task.Tenant -notin $MappedTenants) {
Write-Information "Tenant Removed: Cleaning up scheduled task $($Task.Name) for tenant $($Task.TenantFilter)"
$Entity = $Task | Select-Object -Property PartitionKey, RowKey
Remove-AzDataTableEntity @ScheduledTasksTable -Entity $Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Sync-CippExtensionData {
Error = ''
LastSync = 'Never'
}
Add-CIPPAzDataTableEntity @Table -Entity $LastSync
$null = Add-CIPPAzDataTableEntity @Table -Entity $LastSync
}

try {
Expand Down Expand Up @@ -68,12 +68,12 @@ function Sync-CippExtensionData {
@{
id = 'OneDriveUsage'
method = 'GET'
url = "reports/getOneDriveUsageAccountDetail(period='D7')?`$format=application/json"
url = "reports/getOneDriveUsageAccountDetail(period='D7')?`$format=application%2fjson"
},
@{
id = 'MailboxUsage'
method = 'GET'
url = "reports/getMailboxUsageDetail(period='D7')?`$format=application/json"
url = "reports/getMailboxUsageDetail(period='D7')?`$format=application%2fjson"
}
)

Expand Down Expand Up @@ -172,29 +172,63 @@ function Sync-CippExtensionData {
RowKey = 'Mailboxes'
Data = [string]($Mailboxes | ConvertTo-Json -Depth 10 -Compress)
}
Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
$null = Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force

$SingleGraphQueries = @(
@{
id = 'CASMailbox'
graphRequest = @{
uri = "https://outlook.office365.com/adminapi/beta/$($tenantfilter)/CasMailbox"
Tenantid = $tenantfilter
scope = 'ExchangeOnline'
noPagination = $true
}
}
)

# Bulk request mailbox permissions using New-ExoBulkRequest for each mailbox - mailboxPermissions is not a valid graph query
$ExoBulkRequests = foreach ($Mailbox in $Mailboxes) {
@{
CmdletInput = @{
CmdletName = 'Get-MailboxPermission'
Parameters = @{ Identity = $Mailbox.UPN }
}
}
}
$MailboxPermissions = New-ExoBulkRequest -cmdletArray @($ExoBulkRequests) -tenantid $TenantFilter
$Entity = @{
PartitionKey = $TenantFilter
SyncType = 'Mailboxes'
RowKey = 'MailboxPermissions'
Data = [string]($MailboxPermissions | ConvertTo-Json -Depth 10 -Compress)
}
$null = Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
}
}

if ($TenantRequests) {
Write-Information "Requesting tenant information for $TenantFilter $SyncType"
try {
$TenantResults = New-GraphBulkRequest -Requests $TenantRequests -tenantid $TenantFilter
$TenantResults = New-GraphBulkRequest -Requests @($TenantRequests) -tenantid $TenantFilter
} catch {
Throw "Failed to fetch bulk company data: $_"
}

if ($SingleGraphQueries) {
foreach ($SingleGraphQuery in $SingleGraphQueries) {
$Request = $SingleGraphQuery.graphRequest
$Data = New-GraphGetRequest @Request -tenantid $TenantFilter
$Entity = @{
PartitionKey = $TenantFilter
SyncType = $SyncType
RowKey = $SingleGraphQuery.id
Data = [string]($Data | ConvertTo-Json -Depth 10 -Compress)
}
Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
$TenantResults | Select-Object id, body | ForEach-Object {
$Data = $_.body.value ?? $_.body
if ($Data -match '^eyJ') {
# base64 decode
$Data = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Data)) | ConvertFrom-Json
$Data = $Data.Value
}

$Entity = @{
PartitionKey = $TenantFilter
RowKey = $_.id
SyncType = $SyncType
Data = [string]($Data | ConvertTo-Json -Depth 10 -Compress)
}
$null = Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
}

if ($AdditionalRequests) {
Expand All @@ -210,32 +244,54 @@ function Sync-CippExtensionData {
}
}
}
#Write-Information ($AdditionalRequestQueries | ConvertTo-Json -Depth 10 -Compress)
if (($AdditionalRequestQueries | Measure-Object).Count -gt 0) {
$AdditionalResults = New-GraphBulkRequest -Requests $AdditionalRequestQueries -tenantid $TenantFilter
$AdditionalResults | ForEach-Object {
$Entity = @{
PartitionKey = $TenantFilter
SyncType = $SyncType
RowKey = '{0}_{1}' -f $ParentId, $_.id
Data = [string]($_.body.value | ConvertTo-Json -Depth 10 -Compress)
try {
$AdditionalResults = New-GraphBulkRequest -Requests @($AdditionalRequestQueries) -tenantid $TenantFilter
} catch {
throw $_
}
if ($AdditionalResults) {
$AdditionalResults | ForEach-Object {
$Data = $_.body.value ?? $_.body
if ($Data -match '^eyJ') {
# base64 decode
$Data = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Data)) | ConvertFrom-Json
$Data = $Data.Value
}
$Entity = @{
PartitionKey = $TenantFilter
SyncType = $SyncType
RowKey = '{0}_{1}' -f $ParentId, $_.id
Data = [string]($Data | ConvertTo-Json -Depth 10 -Compress)
}
try {
$null = Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
} catch {
throw $_
}
}
Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
}

}
}
}
}

$TenantResults | Select-Object id, body | ForEach-Object {
if ($SingleGraphQueries) {
foreach ($SingleGraphQuery in $SingleGraphQueries) {
$Request = $SingleGraphQuery.graphRequest
$Data = New-GraphGetRequest @Request -tenantid $TenantFilter
$Entity = @{
PartitionKey = $TenantFilter
RowKey = $_.id
SyncType = $SyncType
Data = [string]($_.body.value | ConvertTo-Json -Depth 10 -Compress)
RowKey = $SingleGraphQuery.id
Data = [string]($Data | ConvertTo-Json -Depth 10 -Compress)
}
Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
$null = Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force
}
}


$LastSync.LastSync = [datetime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ')
$LastSync.Status = 'Completed'
$LastSync.Error = ''
Expand Down
Loading

0 comments on commit 8ec917e

Please sign in to comment.