Here we will create a runbook in the automation account that will retrieve data from Microsoft Graph using the REST API and export the data to the Azure storage account.
Import Modules
We will need a couple of PowerShell modules added to the automation account for the runbook to use.
In the Azure portal in the automation account, go to the Modules gallery pane
Search for and import the following modules:
Az.Accounts
Az.Storage
MSAL.PS (only required if using a Run as account)
Create a Runbook
Click on the Runbooks pane and choose Create a runbook
Enter a name for the runbook, select PowerShell for the runbook type and click Create
Copy the PowerShell code below into the runbook and edit it as described below
If you wish to test the runbook before publishing to make sure it works, use the Test pane
Alternatively, Publish the runbook, then click Start from the runbook menu. This option will give you the full output of the script.
When ready, Publish the runbook
Export-MSGraphManagedDeviceData
This PowerShell script can be used as-is as the source for your runbook or as simply as an example that you can modify to your requirements.
To use the provided Managed Devices PowerBI template, use this code as-is because the template is configured to use the fields that this script exports, as well as the container and data file names.
Populate the following parameters at the top of the script:
$ResourceGroup. This is the name of the resource group that hosts your storage account in Azure
$StorageAccount. This is the name of the storage account to which you will export data
$Container. The name of the container to use in the storage account
Managed Identity vs Run as account
The script is configured to run using a managed identity, but code is also included to use a Run as account instead. To use a Run as account, in the Authentication section of the script, simply uncomment the code blocks that start with #Run as account, and comment out the sections that start with #Managed Identity.
What the Runbook does
First we authenticate to MS Graph and obtain an access token to make our REST API calls with. We also authenticate to Azure AD in order to send data to the storage account.
We query MS Graph to get a list of managed devices in Intune and page through the results to get the full data set.
We then separate out the managed devices by OS creating arrays for iOS, Android and Windows devices.
Not every property returned by MS Graph is useful to us so we have a list of properties to exclude from the results for each OS. We also add a few properties of our own, in some cases simply expanding out nested results into their own fields and in others we add some calculated values of our own, like days since last sync, for example.
Last, we export the results locally into CSV files, one for each OS, then upload these to our storage account. Each CSV file will be placed in its own folder in the storage account container, for example: