Skip to content

JNUC2019 Lab Session E Quick Report

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

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

Lab Session Contents - 4 - A Quick Report

Previous           TOC           Next

  • 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.

mgd_laptops = JSS::Computer.all_laptops.select { |macinfo| macinfo[:managed] } ;0
# => 0

pp mgd_laptops ;0
# [array of hashes]
# =>0

mgd_laptops.size
# => 85
  • Use the all_laptops method to get the list of laptops

  • But we only want the managed ones, so we use another iterator select to filter them out.

  • Like each select loops thru and passes each hash into a block of code.

  • select remembers the ones where the block evaluated to true

  • our code block look at the :managed value in each Hash, which is true or false

  • select then returns a new array, with only the hashes that were 'true'

  • and we store that in the variable mdg_laptops to use later.

mgd_laptop_users = mgd_laptops.map { |macinfo| macinfo[:username] }  ;0
# => 0

pp mgd_laptop_users ;0
# [array of usernames]
# => 0
  • Now we're using yet another iterator: map

  • This loops thru the Hashes in the Array, building another Array with the value calculated in the code block

  • map then returns the new array

  • Our code block is just extracting the :username value from each Hash

  • We get an Array of usernames, which we store in the var. mgd_laptop_users to use later

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 select the ones that are both managed and supervised

  • saving them in the var. mgd_supd_ipads

  • Now that we've gathered the data we need, lets print our report

mgd_supd_ipads.each do |ipad|
  next unless mgd_laptop_users.include? ipad[:username]
  puts "iPad: '#{ipad[:name]}' SN: '#{ipad[:serial_number]}' User: '#{ipad[:username]}' has a managed Mac laptop"
end ;0
# [lines of output]
# => 0
  • Use each to loop through our managed, supervised ipads

  • we skip the ipad unless its user's name is in the list of laptop usernames we saved above

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

Previous           TOC           Next

Clone this wiki locally