Public/AzureAD/Invoke-MsGraphApi.ps1
function Invoke-MsGraphApi { param ( [Parameter(Mandatory = $true)]$Token, [Parameter(Mandatory = $true)][ValidateSet('GET', 'POST', 'PATCH', 'DELETE', 'PUT')][string]$Method, [Parameter(Mandatory = $true)][string]$ApiUrl, [Parameter(Mandatory = $false)][string]$JsonBody ) try { $header = @{Authorization = "Bearer $($Token.access_token)" } Write-Log "$Method : $ApiUrl" $requestParams = @{ Headers = $header Uri = $ApiUrl Method = $Method ContentType = "application/json" UseBasicParsing = $true } if ($PSBoundParameters.Keys -contains 'JsonBody' -and $Method -ne 'GET') { $requestParams += @{Body = $JsonBody} } $request = Invoke-RestMethod @requestParams $returnValue = @() if ($request.value.Count -gt 0) { $returnValue += $request.value Write-Log "Returned Object Count: $($returnValue.Count)" while ($request.'@odata.nextLink' -match 'http') { $requestParams.Uri = $request.'@odata.nextLink' Write-Log "$Method : $($requestParams.Uri)" $request = Invoke-RestMethod @requestParams 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 $_ } } |