Skip to content

2023 demo E Quick Report

Chris Lasell edited this page Aug 28, 2023 · 3 revisions

Hands On, Real-time Jamf APIs Using ruby-jss

A Quick Report

Previous           TOC           Next


A formatted report

  • The Jamf Pro Web UI can automate the generation of some reports

    • But occasionally you'll want something a bit more complex
  • For example, a report of:

    • device name, serial, and username
    • for all iPads that are
      • managed & supervised
      • whose users also have a managed Mac laptop
  • Let's extract that data now


  • First we need a list of all the managed laptops:
mgd_laptops = Jamf::Computer.all_laptops.select { |macinfo| macinfo[:managed] } ;0
# => 0
  • mgd_laptops is a variable in which we'll store some data to use later

    • You assign a value to a variable using =
  • Use the all_laptops method on the Jamf::Computer class to get the array of summary-hashs of the laptops

  • But we only want the managed ones, so we use another iterator method select to filter the list

    • Like each, select loops thru and passes each item of the array into a block of code
    • select remembers the ones where the block evaluates 'truthy'
      • the value of the block is the value returned by its last line, or the value returned by next or break if you use them.
      • when evaluating boolean values, only false and nil are 'falsey', everything else is 'truthy', including 0 and the empty-string.
    • Our code block just looks at the :managed value in each hash, which contains boolean true or false
    • select then returns a new array, with only the hashes that were true
  • We store the filtered array in our variable to use in the next step

  • Look at the contents of mdg_laptops and how many laptops it contains:

pp mgd_laptops.sample ;0
# [Hash of device info]
# =>0

mgd_laptops.size
# => 85

  • But we really want an array of usernames, not an array of hashes, so:
mgd_laptop_users = mgd_laptops.map { |macinfo| macinfo[:username] }.uniq  ;0
# => 0
  • Here we're using yet another iterator, map

    • This loops thru the mgd_laptops array, building a new array with values calculated in the code block
      • The value of the block is the value used in the new array
      • The arrays are in the same order: each item in the new array is 'mapped' to the corresponding item in the original array
    • The map method returns the new array
    • Our code block is just extracting the :username value from each hash
  • We store the array of usernames in the variable mgd_laptop_users to use later

  • Here it is:

pp mgd_laptop_users ;0
# [array of usernames]
# => 0

  • Now its time to get our list of iPads, we need all the ones that are managed and supervised
mgd_supd_ipads = Jamf::MobileDevice.all_ipads.select { |ipad| ipad[:managed] && ipad[:supervised] } ;0
# => 0
  • We use select again, on the array of all_ipads to get the hashes for those that are both managed and supervised

  • We save the new array in the variable mgd_supd_ipads


  • Now that we've gathered the data we need, we can print our report:
mgd_supd_ipads.each do |ipad_data|
  next unless mgd_laptop_users.include? ipad_data[:username]
  
  puts "iPad: '#{ipad_data[:name]}' SN: '#{ipad_data[:serial_number]}' User: '#{ipad_data[:username]}' has a managed Mac laptop"
end ;0
# [lines of output]
# => 0
  • Use each to loop through our array of managed, supervised ipads

  • Use next to skip the ipad unless its user's name is in the array of laptop usernames we saved above

    • Arrays have a method include? that returns true or false
  • Then we print a line of output


Previous           TOC           Next

Clone this wiki locally