enumerate objects from Azure AD via Powershell and the Graph API, and set a delta change cookie
enumerate changes in Azure AD since the last query
return objects that have changed since the last query
return just the changed attributes on objects that have changed since the last query
get a differential sync from now delta change link
Searching through MSDN and other resources working this out I somehow stumbled upon a reference to changes in the API that detail the search filters. v1.5 and later of the API requires filters using the context ‘Microsoft.DirectoryServices.User|Group|Contact’ etc instead of ‘Microsoft.WindowsAzure.ActiveDirectory.User|Group|Contact’ which you’ll find in the few examples around. If you don’t want to return all these object types update the filter on line 21 in the script below.
Here is the script to return all Users, Groups and Contacts from the tenant along with all the other options I detail in this post. Update the following for your tenant;
line 6 for your tenant URI
line 10 for your account in the tenant
line 11 for the password associated with your account from line 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# username and password. The username must be MFA disabled user Admin at least, and must not be a live id.
$username = "doc@mytenant.com.au"
$password = "Sup3rS3cr3t1"
# The resource URI for your token.
$resource = "https://graph.windows.net/"
# Object Type (eg. Users, Groups, Contacts, DirectoryObjects)
$object = "directoryObjects"
# What Objects are we interested in. I'm expliciting calling User, Group and Contact even though they are meant to be implied (default)
# as I've read about mixed results with differential sync across different object types
$Searchfilter ="`$filter=isof('Microsoft.DirectoryServices.User') or isof('Microsoft.DirectoryServices.Group') or isof('Microsoft.DirectoryServices.Contact')"
# Output Directory and file for Differential Cookie
Here is a sample output showing the Users, Groups, Contacts and DirectoryLinkChange objects. Note: if you have a large tenant that has been in place for a period of time it may take a while to enumerate. In this instance you can use the Differential Sync from now option. More on that later.
Running the query again using the Differential DeltaLink from the first run now returns no results. This is as expected as no changes have been made in the tenant on the objects in our query.
What if I just wanted to know the change that was made?
If we add ‘&ocp-aad-dq-include-only-changed-properties=true’ to the URI that’s exactly what we get. The object and what changed. In my case the Department attribute.
Finally as alluded to earlier there is the Differential Sync from now option. Very useful on large tenants where you can query and get all users, contacts, groups etc without using differential sync, then get the Differential Delta token for future sync queries. So I’ve used the same URI that I used as the beginning of this blog post but in the header specified ‘ocp-aad-dq-include-only-delta-token’ = “true” and as you can see I returned no results but I got the important Differential Query DeltaLink.
Using Powershell we can leverage the Azure AD Graph RestAPI and use the Differential Sync functions to efficiently query Azure AD for changes rather than needing to enumerate an entire tenant each time. Brilliant.
I tried this script but I am receiving below error message.
Exception calling “AcquireToken” with “3” argument(s): “AADSTS7000218: The request body must contain the following parameter: ‘client_assertion’ or ‘client_secret’.
Trace ID: 661f5dd1-d985-48ce-8128-dc3918f34500
Correlation ID: bee9612a-1532-4daf-8307-34cab68a6201
Timestamp: 2019-08-12 06:58:43Z”
Please guide me if I am missing anything.
Regards,
Sachin
Comments are closed.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.YesNoPrivacy policy
Hello Darren,
I tried this script but I am receiving below error message.
Exception calling “AcquireToken” with “3” argument(s): “AADSTS7000218: The request body must contain the following parameter: ‘client_assertion’ or ‘client_secret’.
Trace ID: 661f5dd1-d985-48ce-8128-dc3918f34500
Correlation ID: bee9612a-1532-4daf-8307-34cab68a6201
Timestamp: 2019-08-12 06:58:43Z”
Please guide me if I am missing anything.
Regards,
Sachin