|
| 1 | +function Sync-CippExtensionData { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Internal |
| 5 | + #> |
| 6 | + [CmdletBinding()] |
| 7 | + param( |
| 8 | + $TenantFilter, |
| 9 | + $SyncType |
| 10 | + ) |
| 11 | + |
| 12 | + $Table = Get-CIPPTable -TableName ExtensionSync |
| 13 | + $Extensions = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$($SyncType)'" |
| 14 | + $LastSync = $Extensions | Where-Object { $_.RowKey -eq $TenantFilter } |
| 15 | + $CacheTable = Get-CIPPTable -tablename 'CacheExtensionSync' |
| 16 | + |
| 17 | + if (!$LastSync) { |
| 18 | + $LastSync = @{ |
| 19 | + PartitionKey = $SyncType |
| 20 | + RowKey = $TenantFilter |
| 21 | + Status = 'Not Synced' |
| 22 | + Error = '' |
| 23 | + LastSync = 'Never' |
| 24 | + } |
| 25 | + Add-CIPPAzDataTableEntity @Table -Entity $LastSync |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + switch ($SyncType) { |
| 30 | + 'Overview' { |
| 31 | + # Build bulk requests array. |
| 32 | + [System.Collections.Generic.List[PSCustomObject]]$TenantRequests = @( |
| 33 | + @{ |
| 34 | + id = 'TenantDetails' |
| 35 | + method = 'GET' |
| 36 | + url = '/organization' |
| 37 | + }, |
| 38 | + @{ |
| 39 | + id = 'AllRoles' |
| 40 | + method = 'GET' |
| 41 | + url = '/directoryRoles?$top=999' |
| 42 | + }, |
| 43 | + @{ |
| 44 | + id = 'Domains' |
| 45 | + method = 'GET' |
| 46 | + url = '/domains$top=999' |
| 47 | + }, |
| 48 | + @{ |
| 49 | + id = 'Licenses' |
| 50 | + method = 'GET' |
| 51 | + url = '/subscribedSkus?$top=999' |
| 52 | + }, |
| 53 | + @{ |
| 54 | + id = 'Groups' |
| 55 | + method = 'GET' |
| 56 | + url = '/groups?$top=999&$select=id,createdDateTime,displayName,description,mail,mailEnabled,mailNickname,resourceProvisioningOptions,securityEnabled,visibility,organizationId,onPremisesSamAccountName,membershipRule,grouptypes,onPremisesSyncEnabled,resourceProvisioningOptions,userPrincipalName' |
| 57 | + }, |
| 58 | + @{ |
| 59 | + id = 'ConditionalAccess' |
| 60 | + method = 'GET' |
| 61 | + url = '/identity/conditionalAccess/policies' |
| 62 | + }, |
| 63 | + @{ |
| 64 | + id = 'SecureScoreControlProfiles' |
| 65 | + method = 'GET' |
| 66 | + url = '/security/secureScoreControlProfiles?$top=999' |
| 67 | + }, |
| 68 | + @{ |
| 69 | + id = 'Subscriptions' |
| 70 | + method = 'GET' |
| 71 | + url = '/directory/subscriptions?$top=999' |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + $SingleGraphQueries = @(@{ |
| 76 | + id = 'SecureScore' |
| 77 | + graphRequest = @{ |
| 78 | + uri = 'https://graph.microsoft.com/beta/security/secureScores?$top=1' |
| 79 | + noPagination = $true |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + 'Users' { |
| 84 | + [System.Collections.Generic.List[PSCustomObject]]$TenantRequests = @( |
| 85 | + @{ |
| 86 | + id = 'Users' |
| 87 | + method = 'GET' |
| 88 | + url = '/users?$top=999&$select=id,accountEnabled,businessPhones,city,createdDateTime,companyName,country,department,displayName,faxNumber,givenName,isResourceAccount,jobTitle,mail,mailNickname,mobilePhone,onPremisesDistinguishedName,officeLocation,onPremisesLastSyncDateTime,otherMails,postalCode,preferredDataLocation,preferredLanguage,proxyAddresses,showInAddressList,state,streetAddress,surname,usageLocation,userPrincipalName,userType,assignedLicenses,onPremisesSyncEnabled' |
| 89 | + } |
| 90 | + ) |
| 91 | + } |
| 92 | + 'Devices' { |
| 93 | + [System.Collections.Generic.List[PSCustomObject]]$TenantRequests = @( |
| 94 | + @{ |
| 95 | + id = 'Devices' |
| 96 | + method = 'GET' |
| 97 | + url = '/deviceManagement/managedDevices?$top=999' |
| 98 | + }, |
| 99 | + @{ |
| 100 | + id = 'DeviceCompliancePolicies' |
| 101 | + method = 'GET' |
| 102 | + url = '/deviceManagement/deviceCompliancePolicies' |
| 103 | + }, |
| 104 | + @{ |
| 105 | + id = 'DeviceApps' |
| 106 | + method = 'GET' |
| 107 | + url = '/deviceAppManagement/mobileApps' |
| 108 | + } |
| 109 | + ) |
| 110 | + } |
| 111 | + 'Mailboxes' { |
| 112 | + $Select = 'id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox' |
| 113 | + $ExoRequest = @{ |
| 114 | + tenantid = $TenantFilter |
| 115 | + cmdlet = 'Get-Mailbox' |
| 116 | + cmdParams = @{} |
| 117 | + Select = $Select |
| 118 | + } |
| 119 | + $Mailboxes = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ArchiveGuid, WhenSoftDeleted, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } }, |
| 120 | + |
| 121 | + @{ Name = 'displayName'; Expression = { $_.'DisplayName' } }, |
| 122 | + @{ Name = 'primarySmtpAddress'; Expression = { $_.'PrimarySMTPAddress' } }, |
| 123 | + @{ Name = 'recipientType'; Expression = { $_.'RecipientType' } }, |
| 124 | + @{ Name = 'recipientTypeDetails'; Expression = { $_.'RecipientTypeDetails' } }, |
| 125 | + @{ Name = 'AdditionalEmailAddresses'; Expression = { ($_.'EmailAddresses' | Where-Object { $_ -clike 'smtp:*' }).Replace('smtp:', '') -join ', ' } } |
| 126 | + |
| 127 | + $Entity = @{ |
| 128 | + PartitionKey = $TenantFilter |
| 129 | + SyncType = 'Mailboxes' |
| 130 | + RowKey = 'Mailboxes' |
| 131 | + Data = [string]($Mailboxes | ConvertTo-Json -Depth 10 -Compress) |
| 132 | + } |
| 133 | + Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if ($TenantRequests) { |
| 138 | + try { |
| 139 | + $TenantResults = New-GraphBulkRequest -Requests $TenantRequests -tenantid $TenantFilter |
| 140 | + } catch { |
| 141 | + Throw "Failed to fetch bulk company data: $_" |
| 142 | + } |
| 143 | + |
| 144 | + if ($SingleGraphQueries) { |
| 145 | + foreach ($SingleGraphQuery in $SingleGraphQueries) { |
| 146 | + $Request = $SingleGraphQuery.graphRequest |
| 147 | + $Data = New-GraphGetRequest @Request -tenantid $TenantFilter |
| 148 | + $Entity = @{ |
| 149 | + PartitionKey = $TenantFilter |
| 150 | + SyncType = $SyncType |
| 151 | + RowKey = $SingleGraphQuery.id |
| 152 | + Data = [string]($Data | ConvertTo-Json -Depth 10 -Compress) |
| 153 | + } |
| 154 | + Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + $TenantResults | Select-Object id, body | ForEach-Object { |
| 159 | + $Entity = @{ |
| 160 | + PartitionKey = $TenantFilter |
| 161 | + RowKey = $_.id |
| 162 | + SyncType = $SyncType |
| 163 | + Data = [string]($_.body.value | ConvertTo-Json -Depth 10 -Compress) |
| 164 | + } |
| 165 | + Add-CIPPAzDataTableEntity @CacheTable -Entity $Entity -Force |
| 166 | + } |
| 167 | + } |
| 168 | + $LastSync.LastSync = [datetime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ') |
| 169 | + $LastSync.Status = 'Completed' |
| 170 | + $LastSync.Error = '' |
| 171 | + } catch { |
| 172 | + $LastSync.Status = 'Failed' |
| 173 | + $LastSync.Error = [string](Get-CippException -Exception $_ | ConvertTo-Json -Compress) |
| 174 | + throw "Failed to sync data: $($_.Exception.Message)" |
| 175 | + } finally { |
| 176 | + Add-CIPPAzDataTableEntity @Table -Entity $LastSync -Force |
| 177 | + } |
| 178 | +} |
0 commit comments