-
Notifications
You must be signed in to change notification settings - Fork 30
JNUC2019 Lab Session E Quick 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.
-
first we need a list of all the managed laptops
mgd_laptops = JSS::Computer.all_laptops.select { |macinfo| macinfo[:managed] } ;0
# => 0
-
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 totrue
- 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'
- Like
-
and we store that in the variable
mdg_laptops
to use later. -
Lets 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 Array, building a new 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 -
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 ofall_ipads
to get 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 -
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
- Arrays have a method
-
and then we print a line of output