Skip to content

Commit 7a63fe1

Browse files
committed
delta queries
1 parent 63f39b3 commit 7a63fe1

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function New-GraphDeltaQuery {
2+
<#
3+
.SYNOPSIS
4+
Creates a new Graph Delta Query.
5+
.DESCRIPTION
6+
This function creates a new Graph Delta Query to track changes in a specified resource.
7+
.PARAMETER Resource
8+
The resource to track changes for (e.g., 'users', 'groups').
9+
.PARAMETER TenantFilter
10+
The tenant to filter the query on.
11+
#>
12+
[CmdletBinding(DefaultParameterSetName = 'NewDeltaQuery')]
13+
param(
14+
[Parameter(Mandatory = $true, ParameterSetName = 'NewDeltaQuery')]
15+
[Parameter(Mandatory = $true, ParameterSetName = 'DeltaUrl')]
16+
[string]$TenantFilter,
17+
18+
[Parameter(ParameterSetName = 'NewDeltaQuery', Mandatory = $true)]
19+
[ValidateSet('users', 'groups', 'contacts', 'devices', 'applications', 'servicePrincipals', 'directoryObjects', 'administrativeUnits')]
20+
[string]$Resource,
21+
22+
[Parameter(ParameterSetName = 'NewDeltaQuery', Mandatory = $false)]
23+
[hashtable]$Parameters = @{},
24+
25+
[Parameter(ParameterSetName = 'DeltaUrl', Mandatory = $true)]
26+
[string]$DeltaUrl
27+
)
28+
29+
try {
30+
if ($DeltaUrl) {
31+
$GraphQuery = [System.UriBuilder]$DeltaUrl
32+
} else {
33+
$GraphQuery = [System.UriBuilder]('https://graph.microsoft.com/beta/{0}/delta' -f $Resource)
34+
$QueryParams = @{
35+
'$deltaToken' = 'latest'
36+
}
37+
$ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
38+
39+
foreach ($key in $QueryParams.Keys) {
40+
if ($QueryParams[$key]) {
41+
$ParamCollection.Add($key, $QueryParams[$key])
42+
}
43+
}
44+
45+
foreach ($Item in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) {
46+
if ($Item.Value -is [System.Boolean]) {
47+
$Item.Value = $Item.Value.ToString().ToLower()
48+
}
49+
if ($Item.Value) {
50+
$ParamCollection.Add($Item.Key, $Item.Value)
51+
}
52+
}
53+
$GraphQuery.Query = $ParamCollection.ToString()
54+
}
55+
56+
#Write-Information "Creating Delta Query for $Resource with parameters: $($GraphQuery.Query)"
57+
$response = New-GraphGetRequest -tenantid $TenantFilter -uri $GraphQuery.ToString() -ReturnRawResponse
58+
Write-Information "Delta Query created successfully for $Resource. Response: $($response | ConvertTo-Json -Depth 5)"
59+
return $response.Content
60+
} catch {
61+
Write-Error "Failed to create Delta Query: $(Get-NormalizedError -Message $_.Exception.message)"
62+
Write-Warning $_.InvocationInfo.PositionMessage
63+
}
64+
}

Modules/CIPPCore/Public/GraphHelper/New-GraphGetRequest.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function New-GraphGetRequest {
1616
[switch]$ComplexFilter,
1717
[switch]$CountOnly,
1818
[switch]$IncludeResponseHeaders,
19-
[hashtable]$extraHeaders
19+
[hashtable]$extraHeaders,
20+
[switch]$ReturnRawResponse
2021
)
2122

2223
if ($NoAuthCheck -eq $false) {
@@ -65,8 +66,25 @@ function New-GraphGetRequest {
6566
if ($IncludeResponseHeaders) {
6667
$GraphRequest.ResponseHeadersVariable = 'ResponseHeaders'
6768
}
68-
$Data = (Invoke-RestMethod @GraphRequest)
69-
if ($CountOnly) {
69+
70+
if ($ReturnRawResponse) {
71+
$GraphRequest.SkipHttpErrorCheck = $true
72+
$Data = Invoke-WebRequest @GraphRequest
73+
} else {
74+
$Data = (Invoke-RestMethod @GraphRequest)
75+
}
76+
77+
if ($ReturnRawResponse) {
78+
if (Test-Json -Json $Data.Content) {
79+
$Content = $Data.Content | ConvertFrom-Json
80+
$Content
81+
} else {
82+
$Content = $Data.Content
83+
}
84+
85+
$Data | Select-Object -Property StatusCode, StatusDescription, @{Name = 'Content'; Expression = { $Content }}
86+
$nextURL = $null
87+
} elseif ($CountOnly) {
7088
$Data.'@odata.count'
7189
$NextURL = $null
7290
} else {

0 commit comments

Comments
 (0)