logo

NJP

Integrate with Microsoft Intune - Scripted Data Retrieval

Import · Dec 12, 2021 · article

The great thing about ServiceNow is that there are n+1 ways of doing something which allows for creative solutions. The risk and consideration with scripting is ensuring instance performance isn't harmed by creating poor performing code. This scripted approach provides the framework for retrieving information from Microsoft Graph, but isn't designed to be production-ready. If you have any questions please feel free to leave them in the comments section below and I'll be more than happy to advise.

Outbound REST Message

To group all our Intune queries in one location and simplify management, an Outbound REST message record is created which will avoid needing to write large unwieldy scripts.

  • From the application navigator, search for REST Message and create a new record
  • Name the new record something succinct as this name will be used in scripts
  • Set the endpoint to https://graph.microsoft.com/
  • Set the authentication method to OAuth 2.0 and the profile we created in the previous article.
  • Save the record

For this example, we're going to focus on retrieving only computer devices from Intune using a filter on the API query.

  • Create a HTTP method via the related list
  • Set the name to GET Computing Devices
  • Set the endpoint to https://graph.microsoft.com/beta/deviceManagement/managedDevices
  • Save the record
  • To test, we're going to add ?$top=5 to the end of the endpoint to limit the results returned. Use the Test UI action to retrieve the data. If all is setup correctly, you'll receive a 200 HTTP code and a response body of records.

Staging Table

Now to simplify the process for you, we're going to do a bit of SN trickery to save us some time with the setup process and avoid this article becoming a novel. Effectively, we need to create an import set table that has a single JSON column to hold each managedDevice object ready for transforming. Now, for whatever reason, ServiceNow has disabled the option for us to select JSON columns as a type and requires a fix script to run to allow the option. Rather than modifying system records and putting you in uncomfortable territory, we can get ServiceNow to do it for us.

  • Create a new data source record, accessible under System Import Sets
  • Set the name and import set label to Intune Computer Device Imp
  • Set the type to file and format to JSON
  • In the path field enter /results/results
  • Ensure the Data in single column is selected
  • Save the record
  • Open notepad on your PC (or any simple text editor) and create a JSON file type with the below content:
{ "results" : [{} , {} ] }
  • Upload the JSON file to the data source record using the attachment paper clip icon.
  • Use the Load All Records UI Action to generate the table

Create Script Include

Now, finally for some scripting. We need to retrieve the data, load the data into the import table, retrieve more data if available, and finally run the transformation.

  • Download the attached script include and import it into your instance by navigating the Script Include table and right clicking one of the headers. Select Import XML and upload the file.
  • Navigate to the newly inserted IntuneUtils script include.
  • Modify line 32 from Intune Graph API - READ to the name of the Outbound REST Message you created as part of the first step in this article.
  • This script include provides you with the boilerplate of retrieving data and inserting it into the import set table. It's not intended to be error-proof but to provide you with the basics.
  • Run the utility using scripts background and new IntuneUtils("import_table" , "data_source_name" , "odata_filter").run(); replacing the paramters with relevant information.
  • You should now see rows in your import set table that are ready to be transformed!

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/integrate-with-microsoft-intune-scripted-data-retrieval/ta-p/2323650