Public/AzureAD/Invoke-MsGraphApi.ps1
function Invoke-MsGraphApi { param ( [Parameter(Mandatory = $true)]$Token, [Parameter(Mandatory = $true)][ValidateSet('GET', 'POST')][string]$Method, [Parameter(Mandatory = $true)][string]$ApiUrl, [Parameter(Mandatory = $false)][string]$JsonBody ) try { $header = @{Authorization = "Bearer $($Token.access_token)" } Write-Log "$Method : $ApiUrl $($JsonBody | ConvertFrom-Json | ConvertTo-Json -Compress)" $request = Invoke-RestMethod -Headers $header -Uri $ApiUrl -Method $Method -ContentType "application/json" -UseBasicParsing:$true -Body $JsonBody $returnValue = @() if ($request.value.Count -gt 0) { $returnValue += $request.value Write-Log "Returned Object Count: $($returnValue.Count)" while ($request.'@odata.nextLink' -match 'http') { $url = $request.'@odata.nextLink' Write-Log "$Method : $ApiUrl $($JsonBody | ConvertFrom-Json | ConvertTo-Json -Compress)" $request = Invoke-RestMethod -Headers $header -Uri $url -Method $Method -ContentType "application/json" -UseBasicParsing:$true -Body $JsonBody if ($request.value.Count -gt 0) { $returnValue += $request.value Write-Log "Returned Object Count: $($returnValue.Count)" } } Write-Log "Final Object Count: $($returnValue.Count)" return $returnValue } else { return $request } return $request } catch { Write-Log "Unhandled exception" -LogType error -ErrorObject $_ Write-Error $_ } } |