-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathPush-ListGraphRequestQueue.ps1
More file actions
81 lines (72 loc) · 3.39 KB
/
Push-ListGraphRequestQueue.ps1
File metadata and controls
81 lines (72 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Push-ListGraphRequestQueue {
<#
.FUNCTIONALITY
Entrypoint
#>
param($Item)
Write-Information "PowerShell durable function processed work item: $($Item.Endpoint) - $($Item.TenantFilter)"
try {
$ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
$Parameters = $Item.Parameters | ConvertTo-Json -Depth 5 | ConvertFrom-Json -AsHashtable
foreach ($Param in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) {
$ParamCollection.Add($Param.Key, $Param.Value)
}
$PartitionKey = $Item.PartitionKey
$TableName = ('cache{0}' -f ($Item.Endpoint -replace '[^A-Za-z0-9]'))[0..62] -join ''
Write-Information "Queue Table: $TableName"
$Table = Get-CIPPTable -TableName $TableName
$Filter = "PartitionKey eq '{0}' and (RowKey eq '{1}' or OriginalEntityId eq '{1}')" -f $PartitionKey, $Item.TenantFilter
Write-Information "Filter: $Filter"
$Existing = Get-CIPPAzDataTableEntity @Table -Filter $Filter -Property PartitionKey, RowKey, OriginalEntityId
if ($Existing) {
$null = Remove-AzDataTableEntity -Force @Table -Entity $Existing
}
$GraphRequestParams = @{
TenantFilter = $Item.TenantFilter
Endpoint = $Item.Endpoint
Parameters = $Parameters
NoPagination = $Item.NoPagination
ReverseTenantLookupProperty = $Item.ReverseTenantLookupProperty
ReverseTenantLookup = $Item.ReverseTenantLookup
AsApp = $Item.AsApp ?? $false
Caller = 'Push-ListGraphRequestQueue'
SkipCache = $true
}
$RawGraphRequest = try {
$Results = Get-GraphRequestList @GraphRequestParams
if ($Results[-1].PSObject.Properties.Name -contains 'nextLink') {
$Results | Select-Object -First ($Results.Count - 1)
} else {
$Results
}
} catch {
$CippException = Get-CippException -Exception $_.Exception
[PSCustomObject]@{
Tenant = $Item.TenantFilter
CippStatus = "Could not connect to tenant. $($CippException.NormalizedMessage)"
CippException = [string]($CippException | ConvertTo-Json -Depth 10 -Compress)
}
}
$Json = ConvertTo-Json -Depth 10 -Compress -InputObject $RawGraphRequest
$GraphResults = [PSCustomObject]@{
PartitionKey = [string]$PartitionKey
RowKey = [string]$Item.TenantFilter
QueueId = [string]$Item.QueueId
QueueType = [string]$Item.QueueType
Data = [string]$Json
}
Add-CIPPAzDataTableEntity @Table -Entity $GraphResults -Force | Out-Null
if ($env:CIPPNG -eq 'true') {
try {
[Craft.Services.CacheBridge]::InvalidateByScope('AllTenants')
} catch {
Write-Information "CacheBridge invalidation skipped: $($_.Exception.Message)"
}
}
return $true
} catch {
Write-Warning "Queue Error: $($_.Exception.Message)"
#Write-Information ($GraphResults | ConvertTo-Json -Depth 10 -Compress)
throw $_
}
}