|
| 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 | +} |
0 commit comments