logo

NJP

Update your SNow CMDB from Airwatch

Import · Apr 29, 2015 · article

I would like to start off by thanking my fellow community members and stackoverflow.com who have been my reference in building these two scripts.

This integration is built through REST API calls.

  1. You will need to create an account in Airwatch and allow it API access. You will also need your Airwatch tenant code as it will be an HTTP header in your rest call.
  2. Build a basic REST Outbound call.
  3. Set your HTTP headers of aw-tenant-code="your_AW_Tenant_code", Accept=application/json, and Content-Type=application/json.
  4. These integrations only use the "get" Method.
  5. For the script to pull the full CMDB of only iPhones use the endpoint: https://.airwatchportals.com/API/v1/mdm/devices/search?model=iPhone
    -you can replace iPhone with any other value that matches the model of the device, or remove "model=iPhone" for a full inventory.
  6. For the script to pull a specific record from a UI Action use the endpoint:
    https://.airwatchportals.com/api/v1/mdm/devices/serialnumber/${SerialNumber}
    -the script will provide the open record's serial number to Airwatch to collect the data for that device.
  7. You will need a new inbound Web Service to send the data to. This Web service should contains the fields you are interested in from Airwatch.
    (Run the test from the REST call to receive a sample of what fields and data are brought over.)
  8. The Web Service will also need a transform map to map those fields to the actual CMDB table you want the devices in. Make sure you coalesce on a unique fields like serial number.
  9. I have made a Mobile Devices table which extends the table Hardware and added all the Airwatch fields. From it I created 3 more, iPhones, iPads, and Androids. And as such have three Web Services to map each to their table.

Below are two scripts, one to pull only one device using its serial number, and the other to run the full iPhone pull.

Airwatch pull using UI action for single device:

try {

      //Initiate REST call and provide this record's serial number

      var r = new sn_ws.RESTMessageV2('AW CI Query', 'get');

      r.setStringParameter('SerialNumber', current.serial_number);

      var response = r.execute();

      var responseBody = response.getBody();

      var httpStatus = response.getStatusCode();

      //Logging or clowns will eat me

      gs.log("REST Call, AW iPhone Pull for " + current.serial_number + " terminated with: " + httpStatus);

      /*Uncomment for debugging

      gs.log("REST Call, AW pull from CI, has a body of:\n" + responseBody);

      */

      //Parse the JSON body returned by AW

      var parsed_object = JSON.parse(responseBody);

      //create glide records to import table

      var gr = new GlideRecord('u_iphone_pull');

      gr.initialize();

      //Map JSON fields to import table fields

      gr.u_assetnumber = parsed_object.AssetNumber;

      gr.u_compliancestatus = parsed_object.ComplianceStatus;

      gr.u_compliancesummary = parsed_object.ComplianceSummary;

      gr.u_compromisedstatus = parsed_object.CompromisedStatus;

      gr.u_devicefriendlyname = parsed_object.DeviceFriendlyName;

      gr.u_enrollmentstatus = parsed_object.EnrollmentStatus;

      gr.u_imei = parsed_object.Imei;

      gr.u_isremotemanagementenabled = parsed_object.IsRemoteManagementEnabled;

      gr.u_issupervised = parsed_object.IsSupervised;

      gr.u_lastcompliancecheckon = parsed_object.LastComplianceCheckOn;

      gr.u_lastcompromisedcheckon = parsed_object.LastCompromisedCheckOn;

      gr.u_lastenrolledon = parsed_object.LastEnrolledOn;

      gr.u_lastseen = parsed_object.LastSeen;

      gr.u_locationgroupname = parsed_object.LocationGroupName;

      gr.u_macaddress = parsed_object.MacAddress;

      gr.u_model = parsed_object.Model;

      gr.u_operatingsystem = parsed_object.OperatingSystem;

      gr.u_ownership = parsed_object.Ownership;

      gr.u_phonenumber = parsed_object.PhoneNumber;

      gr.u_platform = parsed_object.Platform;

      gr.u_serialnumber = parsed_object.SerialNumber;

      gr.u_udid = parsed_object.Udid;

      gr.u_useremailaddress = parsed_object.UserEmailAddress;

      gr.u_username = parsed_object.UserName;

      /*Uncomment for debugging

      gs.log("Serial being sent to the map: " + parsed_object.SerialNumber);

      */

      //GFABS!

      gr.insert();

}

catch(ex) {

      var message = ex.getMessage();

}

Airwatch full pull parsing through JSON array:

try {

      // Call the REST Message

      var r = new RESTMessage('AW iPhone Pull', 'get');

      var response = r.execute();

      var responseBody = response.getBody();

      gs.log("REST Call success");

      //Parsing the Response

      var parser = new JSONParser();

      var parsed = parser.parse(responseBody);

      //Parse based on the array object named Devices and save to variable "cmdb"

      var cmdb = parsed["Devices"];

      //Loop through the array

      for (var key in cmdb) {

      if (cmdb.hasOwnProperty(key)){

              var parsed_object = cmdb[key];

      //Create a new record on each loop

      var gr = new GlideRecord('u_iphone_pull');

      gr.initialize();

      //Map JSON fields to web service table fields

      gr.u_assetnumber = parsed_object.AssetNumber;

      gr.u_compliancestatus = parsed_object.ComplianceStatus;

      gr.u_compliancesummary = parsed_object.ComplianceSummary;

      gr.u_compromisedstatus = parsed_object.CompromisedStatus;

      gr.u_devicefriendlyname = parsed_object.DeviceFriendlyName;

      gr.u_enrollmentstatus = parsed_object.EnrollmentStatus;

      gr.u_imei = parsed_object.Imei;

      gr.u_isremotemanagementenabled = parsed_object.IsRemoteManagementEnabled;

      gr.u_issupervised = parsed_object.IsSupervised;

      gr.u_lastcompliancecheckon = parsed_object.LastComplianceCheckOn;

      gr.u_lastcompromisedcheckon = parsed_object.LastCompromisedCheckOn;

      gr.u_lastenrolledon = parsed_object.LastEnrolledOn;

      gr.u_lastseen = parsed_object.LastSeen;

      gr.u_locationgroupname = parsed_object.LocationGroupName;

      gr.u_macaddress = parsed_object.MacAddress;

      gr.u_model = parsed_object.Model;

      gr.u_operatingsystem = parsed_object.OperatingSystem;

      gr.u_ownership = parsed_object.Ownership;

      gr.u_phonenumber = parsed_object.PhoneNumber;

      gr.u_platform = parsed_object.Platform;

      gr.u_serialnumber = parsed_object.SerialNumber;

      gr.u_udid = parsed_object.Udid;

      gr.u_useremailaddress = parsed_object.UserEmailAddress;

      gr.u_username = parsed_object.UserName;

      /*Uncomment for debugging

      gs.log("Serial being sent to the map: " + parsed_object.SerialNumber);

      */

      //GFABS!

      gr.insert();

      }

}

catch(ex) {

      var message = ex.getMessage();

}

I hope this allows you to build a basic Airwatch integration. And hopefully provide a good understanding on how to pull data in to SNow via REST.

View original source

https://www.servicenow.com/community/developer-articles/update-your-snow-cmdb-from-airwatch/ta-p/2323007