Skip to content

JNUC2019 Lab Session I Changing EAs

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

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

(Home)

Lab Session Contents - 8 - 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 = 'JNUC-2019-LabUser'
# => "JNUC-2019-LabUser"
  • Now put your desired value into a variable also, use any text you'd like
ea_value = 'Jeaux Bleaux'
# => "Jeaux Bleaux"
  • 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 = JSS::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}' "

  sleep (rand + 1)
end
# [ lines of output]
# => [ array of member ids, from 'each' ]
  • In the loop, we fetch the JSS::Computer object by its id

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

    • JSS::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

    • JSS::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 = JSS::Computer.fetch id: my_grp.member_ids.sample ;0
# => 0

comp_to_check.ext_attrs[ea_name]
# => "Jeaux Bleaux"

Previous           TOC           Next

Clone this wiki locally