|
| 1 | +function Invoke-ExecGraphRequestProfile { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + CIPP.Core.Read |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $TenantFilter = $Request.Query.tenantFilter |
| 12 | + $Endpoint = $Request.Query.Endpoint |
| 13 | + if (!$TenantFilter -or !$Endpoint) { |
| 14 | + return [HttpResponseContext]@{ |
| 15 | + StatusCode = 400 |
| 16 | + Body = @{ error = 'tenantFilter and Endpoint are required' } | ConvertTo-Json |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + $Timings = [System.Collections.Generic.List[object]]::new() |
| 21 | + $OverallSw = [System.Diagnostics.Stopwatch]::StartNew() |
| 22 | + |
| 23 | + function Add-Timing($Step, $Detail, $ElapsedMs) { |
| 24 | + $Timings.Add([PSCustomObject]@{ |
| 25 | + Step = $Step |
| 26 | + Detail = $Detail |
| 27 | + Ms = [math]::Round($ElapsedMs, 1) |
| 28 | + WallClock = [math]::Round($OverallSw.Elapsed.TotalMilliseconds, 1) |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + # ── 1. Parameter setup ────────────────────────────────────────────── |
| 33 | + $sw = [System.Diagnostics.Stopwatch]::StartNew() |
| 34 | + $Parameters = @{} |
| 35 | + foreach ($key in @('$filter', 'graphFilter', '$select', '$expand', 'expand', '$top', '$count', '$orderby', '$search', '$format')) { |
| 36 | + if ($Request.Query.$key) { |
| 37 | + if ($key -eq 'graphFilter') { $Parameters.'$filter' = $Request.Query.$key } |
| 38 | + elseif ($key -eq '$count') { $Parameters.$key = ([string]([System.Boolean]$Request.Query.$key)).ToLower() } |
| 39 | + else { $Parameters.$key = $Request.Query.$key } |
| 40 | + } |
| 41 | + } |
| 42 | + $sw.Stop() |
| 43 | + Add-Timing 'ParameterSetup' 'Extracted query params' $sw.Elapsed.TotalMilliseconds |
| 44 | + |
| 45 | + # ── 2. Graph URL build ────────────────────────────────────────────── |
| 46 | + $sw.Restart() |
| 47 | + $Version = if ($Request.Query.Version) { $Request.Query.Version } else { 'beta' } |
| 48 | + $Endpoint = $Endpoint -replace '^/', '' |
| 49 | + $GraphQuery = [System.UriBuilder]('https://graph.microsoft.com/{0}/{1}' -f $Version, $Endpoint) |
| 50 | + $ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) |
| 51 | + foreach ($Item in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) { |
| 52 | + $val = $Item.Value |
| 53 | + if ($val -is [System.Boolean]) { $val = $val.ToString().ToLower() } |
| 54 | + if ($val) { $ParamCollection.Add($Item.Key, $val) } |
| 55 | + } |
| 56 | + $GraphQuery.Query = $ParamCollection.ToString() |
| 57 | + $GraphUrl = $GraphQuery.ToString() |
| 58 | + $sw.Stop() |
| 59 | + Add-Timing 'UrlBuild' $GraphUrl $sw.Elapsed.TotalMilliseconds |
| 60 | + |
| 61 | + # ── 3. Get-CIPPAuthentication (env check) ─────────────────────────── |
| 62 | + $sw.Restart() |
| 63 | + $envPresent = [bool]$env:ApplicationID -and [bool]$env:ApplicationSecret -and [bool]$env:RefreshToken |
| 64 | + if (!$env:SetFromProfile) { |
| 65 | + Get-CIPPAuthentication | Out-Null |
| 66 | + } |
| 67 | + $sw.Stop() |
| 68 | + Add-Timing 'GetCIPPAuthentication' "EnvPresent=$envPresent SetFromProfile=$($env:SetFromProfile)" $sw.Elapsed.TotalMilliseconds |
| 69 | + |
| 70 | + # ── 4. CIPPCore module state ──────────────────────────────────────── |
| 71 | + $sw.Restart() |
| 72 | + $scope = 'https://graph.microsoft.com/.default' |
| 73 | + # Match Get-GraphToken's key format: $asApp is $null when not passed, so key ends with empty string |
| 74 | + $TokenKeyNull = '{0}-{1}-{2}' -f $TenantFilter, $scope, $null |
| 75 | + $TokenKeyFalse = '{0}-{1}-{2}' -f $TenantFilter, $scope, $false |
| 76 | + $coreState = & (Get-Module CIPPCore) { |
| 77 | + $keyNull = $args[0] |
| 78 | + $keyFalse = $args[1] |
| 79 | + $cachedNull = $script:AccessTokens.$keyNull |
| 80 | + $cachedFalse = $script:AccessTokens.$keyFalse |
| 81 | + $now = [int](Get-Date -UFormat %s -Millisecond 0) |
| 82 | + @{ |
| 83 | + TokenCachedNull = [bool]($cachedNull -and $now -lt $cachedNull.expires_on) |
| 84 | + TokenCachedFalse = [bool]($cachedFalse -and $now -lt $cachedFalse.expires_on) |
| 85 | + CacheKeys = if ($script:AccessTokens) { @($script:AccessTokens.Keys) } else { @() } |
| 86 | + CacheType = if ($script:AccessTokens) { $script:AccessTokens.GetType().Name } else { 'null' } |
| 87 | + CacheCount = if ($script:AccessTokens) { $script:AccessTokens.Count } else { 0 } |
| 88 | + LoginSession = [bool]$script:LoginWebSession |
| 89 | + GraphSession = [bool]$script:GraphWebSession |
| 90 | + } |
| 91 | + } $TokenKeyNull $TokenKeyFalse |
| 92 | + $sw.Stop() |
| 93 | + Add-Timing 'CoreModuleState' "CacheCount=$($coreState.CacheCount) Keys=$($coreState.CacheKeys -join ';') LoginSession=$($coreState.LoginSession) GraphSession=$($coreState.GraphSession)" $sw.Elapsed.TotalMilliseconds |
| 94 | + |
| 95 | + # ── 5. Get-AuthorisedRequest ──────────────────────────────────────── |
| 96 | + $sw.Restart() |
| 97 | + $isAuth = Get-AuthorisedRequest -Uri $GraphUrl -TenantID $TenantFilter |
| 98 | + $sw.Stop() |
| 99 | + Add-Timing 'GetAuthorisedRequest' "Authorised=$isAuth" $sw.Elapsed.TotalMilliseconds |
| 100 | + |
| 101 | + # ── 6. Get-GraphToken ─────────────────────────────────────────────── |
| 102 | + $sw.Restart() |
| 103 | + $headers = Get-GraphToken -tenantid $TenantFilter -scope $scope |
| 104 | + $sw.Stop() |
| 105 | + Add-Timing 'GetGraphToken' "HasAuth=$([bool]$headers.Authorization)" $sw.Elapsed.TotalMilliseconds |
| 106 | + |
| 107 | + # ── 7. Raw Invoke-RestMethod (baseline — no wrapper overhead) ────── |
| 108 | + $sw.Restart() |
| 109 | + $directRequest = @{ |
| 110 | + Uri = $GraphUrl |
| 111 | + Method = 'GET' |
| 112 | + Headers = $headers |
| 113 | + ContentType = 'application/json; charset=utf-8' |
| 114 | + } |
| 115 | + if ($coreState.GraphSession) { |
| 116 | + $graphSess = & (Get-Module CIPPCore) { $script:GraphWebSession } |
| 117 | + if ($graphSess) { $directRequest.WebSession = $graphSess } |
| 118 | + } |
| 119 | + $directResult = Invoke-RestMethod @directRequest |
| 120 | + $directCount = if ($directResult.value) { $directResult.value.Count } else { 1 } |
| 121 | + $sw.Stop() |
| 122 | + Add-Timing 'DirectInvokeRestMethod' "ResultCount=$directCount" $sw.Elapsed.TotalMilliseconds |
| 123 | + |
| 124 | + # ── 8. Get-GraphRequestList (full wrapper) ────────────────────────── |
| 125 | + $sw.Restart() |
| 126 | + $ManualPagination = [System.Boolean]$Request.Query.manualPagination |
| 127 | + $listParams = @{ |
| 128 | + TenantFilter = $TenantFilter |
| 129 | + Endpoint = $Request.Query.Endpoint |
| 130 | + Parameters = ($Parameters.Clone()) |
| 131 | + ManualPagination = $ManualPagination |
| 132 | + SkipCache = $true |
| 133 | + } |
| 134 | + $listParams.Parameters.Remove('$count') |
| 135 | + $listResult = Get-GraphRequestList @listParams |
| 136 | + $listCount = if ($listResult -is [array]) { $listResult.Count } else { 1 } |
| 137 | + $sw.Stop() |
| 138 | + Add-Timing 'GetGraphRequestList' "ResultCount=$listCount" $sw.Elapsed.TotalMilliseconds |
| 139 | + |
| 140 | + # ── 9. CIPPCore state after calls ─────────────────────────────────── |
| 141 | + $sw.Restart() |
| 142 | + $coreStateAfter = & (Get-Module CIPPCore) { |
| 143 | + $keyNull = $args[0] |
| 144 | + $now = [int](Get-Date -UFormat %s -Millisecond 0) |
| 145 | + $cached = $script:AccessTokens.$keyNull |
| 146 | + @{ |
| 147 | + TokenCached = [bool]($cached -and $now -lt $cached.expires_on) |
| 148 | + CacheCount = if ($script:AccessTokens) { $script:AccessTokens.Count } else { 0 } |
| 149 | + CacheKeys = if ($script:AccessTokens) { @($script:AccessTokens.Keys) } else { @() } |
| 150 | + LoginSession = [bool]$script:LoginWebSession |
| 151 | + GraphSession = [bool]$script:GraphWebSession |
| 152 | + } |
| 153 | + } $TokenKeyNull |
| 154 | + $sw.Stop() |
| 155 | + Add-Timing 'CoreStateAfter' "TokenCached=$($coreStateAfter.TokenCached) CacheCount=$($coreStateAfter.CacheCount) LoginSession=$($coreStateAfter.LoginSession) GraphSession=$($coreStateAfter.GraphSession)" $sw.Elapsed.TotalMilliseconds |
| 156 | + |
| 157 | + $OverallSw.Stop() |
| 158 | + Add-Timing 'Total' 'End-to-end' $OverallSw.Elapsed.TotalMilliseconds |
| 159 | + |
| 160 | + $ProfileData = [PSCustomObject]@{ |
| 161 | + Tenant = $TenantFilter |
| 162 | + Endpoint = $Request.Query.Endpoint |
| 163 | + GraphUrl = $GraphUrl |
| 164 | + CoreStateBefore = $coreState |
| 165 | + CoreStateAfter = $coreStateAfter |
| 166 | + Timings = @($Timings) |
| 167 | + } |
| 168 | + |
| 169 | + return [HttpResponseContext]@{ |
| 170 | + StatusCode = 200 |
| 171 | + Body = $ProfileData |
| 172 | + } |
| 173 | +} |
0 commit comments