Generate reports through CLI or CRON using PHP and MS Graph.
This application uses the client credential flow to authorize the application to anonymously access information about your tenant without having a user present. This means we can grab all sorts of useful information about the state of our tenant and compile reports, push actions such as creating new Planner boards, and send emails.
In this sample, we use the PHP client library to authorize our application, then grab data from Groups, Users, OneDrive, and SharePoint, and compile a summary email to the admin.
To use this script, you will need to have:
- An O365 subscription on a valid tenant
- An application registered at apps.dev.microsoft.com
- Access to an admin account for the specified tenant
- Go to apps.dev.microsoft.com and click "Register Your App"
- Select "Web application" (even though this will be a script, we will do a one-time authorization with an admin on behalf of your tenant)
- Give your application a name and enter your email address
- Put in a redirect URI. This can be anything since we don't need to get an access token from the authorization process. I just entered
http://localhost
- Under Application Secrets, click "Generate New Password" and copy the value to the report-generator.php file
- The Application Permissions section defines which privileges your application as a whole has, whereas delegated permissions define which privileges an individual user logged into your app has. Since there won't be a user present, we need to ask for application permissions. At a minimum, you'll want to select
- Directory.Read.All
- Group.Read.All
- Mail.Read
- Mail.Send
- User.Read.All
- Click Save
- Copy the Application ID into the report-generator.php file
- Copy the Application ID and Secret into the corresponding variables in the script
- Enter the email address you would like to send the report to
- Enter the tenant name or ID you wish to collect data on
The first time you want to use your application, you will need to grant the application permission to your tenant as an administrator. This is easily achieved by going to
https://login.microsoftonline.com/{tenant}/adminconsent?client_id={client_id}&state=12345&redirect_uri=http://localhost:8000
in your web browser, signing in as a tenant admin, and accepting the scopes we previously requested. This will not need to be done again.
Install Composer dependency with composer install
Call php report-generator.php
from the CLI