Skip to content

JNUC2019 Lab Session E Quick Report

Chris Lasell edited this page Oct 31, 2019 · 10 revisions

Hands On, Real-time Classic API Using ruby-jss

(Home)

Lab Session Contents - 4 - A Quick Report

Previous           TOC           Next


A formatted report

  • The Jamf Pro 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 = JSS::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 JSS::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 evaluated to true
    • Our code block just looks at the :managed value in each hash, which contains 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 ;0
# [array of hashes]
# =>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] }  ;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
      • 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 = JSS::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