DanielChronlund / DCToolbox

Tools for Microsoft cloud fans
309 stars 52 forks source link

Never-ending Graph API query #3

Closed ivandretvic closed 3 years ago

ivandretvic commented 3 years ago

When i perform the below query i get duplicate results 2294 times. i.e. The full query was performed that many times.

https://graph.microsoft.com/beta/users?$select=userprincipalname,displayName,signInActivity,onPremisesDistinguishedName,officeLocation,jobTitle,department,Country,accountEnabled'

using the following command: $GraphUri = 'https://graph.microsoft.com/beta/users?$select=userprincipalname,displayName,signInActivity,onPremisesDistinguishedName,officeLocation,jobTitle,department,Country,accountEnabled' Invoke-DCMsGraphQuery -AccessToken $AccessToken -GraphMethod 'GET' -GraphUri $GraphUri

Running a count query in Microsoft Graph explorer https://graph.microsoft.com/beta/users/$count produces a count of 3497.

When running this command below, and following the links, i only produce 4 pages worth or results: https://graph.microsoft.com/beta/users?$top=999&$select=userprincipalname%2cdisplayName%2csignInActivity%2conPremisesDistinguishedName%2cofficeLocation%2cjobTitle%2cdepartment%2cCountry%2caccountEnabled

There looks to be an issue with a loop or validation in the while statements.

ivandretvic commented 3 years ago

looking further, the following code doesnt match up with much else:

$uri = $Results.'@odata.nextlink' } until (!($uri))

There should be an execution of the the URI as a separate invoke command specific for that URI, like in the following example:

$Uri = "https://graph.microsoft.com/v1.0/auditLogs/signIns"

#Fetch all security alerts
$AuditLogRequest = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Get -ContentType "application/json"

$AuditLogs = @()
$AuditLogs+=$AuditLogRequest.value

while($AuditLogRequest.'@odata.nextLink' -ne $null) {
    $AuditLogRequest += Invoke-RestMethod -Uri $AuditLogRequest.'@odata.nextLink' -Headers $Header -Method Get -ContentType "application/json"
}

https://adamtheautomator.com/microsoft-graph-api-powershell/

ivandretvic commented 3 years ago

Looking at the code I’m pretty sure you fixed it. I’ll test it tomorrow. I stumbled on this code and it’s very elegant. Probably work just making it a dependency or forking it. https://github.com/Freakling/Powershell-MicrosoftGraph