Skip to content

2023 demo I Changing EAs

Chris Lasell edited this page Aug 24, 2023 · 1 revision

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

Changing an Extention Attribute for the Computers in a Group

Previous           TOC           Next


Modifying extension attribute values

  • Here's something a bit more practical

    • Lets change an extension attribute value for all the members of our group
  • This would work for smart groups, as well as static groups

  • Here's the extension attribute we'll change

    • put its name into a variable:
ea_name = 'ruby-jss-demo'
# => "ruby-jss-demo"
  • Now put our desired value into a variable also
ea_value = 'This computer is awesome'
# => 'This computer is awesome'
  • Now we loop through the group members using each on the array of member_ids from the group:
my_grp.member_ids.each do |comp_id|
  computer = Jamf::Computer.fetch id: comp_id

  orig_val = computer.ext_attrs[ea_name]

  computer.set_ext_attr ea_name, ea_value

  computer.save

  puts "Changed EA '#{ea_name}' from '#{orig_val}' to '#{ea_value}' for computer '#{computer.name}' "
end
# [ lines of output]
# => [ array of member ids, from 'each' ]
  • In the loop, we fetch the Jamf::Computer object by its id

  • We store the original value of the EA in a variable, for reporting

    • Jamf::Computer objects have a method ext_attrs that returns a hash of EA names (hash keys) to EA values (hash values)
    • For us, the original EA value will be blank, since we just created these computer records
  • We then set the EA to the desired value

    • Jamf::Computer objects have a method set_ext_attr that takes two parameters, the EA name, and the new value
  • Save our changes for this computer

  • Print out a line saying what we did

  • To check our work, re-fetch one of the computers, and look at the value

comp_to_check = Jamf::Computer.fetch id: my_grp.member_ids.sample ;0
# => 0

comp_to_check.ext_attrs[ea_name]
# => "This computer is awesome"

Previous           TOC           Next

Clone this wiki locally