-
Notifications
You must be signed in to change notification settings - Fork 30
JNUC2019 Lab Session I Changing EAs
-
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.
-
First, fetch the group you're interested in, we'll use the group we just created
group = JSS::ComputerGroup.fetch name: grp_name ;0
# => 0
-
Put the name and desired value for the ext. attrib. into variables.
- This EA already exists, so use this one
-
Use anything you want for the value
ea_name = 'JNUC-2019-LabUser'
# => "JNUC-2019-LabUser"
ea_value = 'Chris Lasell'
# => "Chris Lasell"
- and now we loop through the group members using
each
on the array ofmember_ids
from the group
group.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}' "
end
# [ lines of output]
# => [ array of member ids, from 'each' ]
-
In the loop, we fetch the computer object by its id
-
we store the original value of the EA in a variable, for reporting
- for us, it will be blank, since we just created these computers
-
we then set the EA to the desired value
-
and save our changes
-
To check our work, re-fetch one of the computers, and look at the value
comp_to_check = JSS::Computer.fetch id: group.member_ids.sample ;0
# => 0
comp_to_check.ext_attrs[ea_name]
# => "Chris Lasell"
-
The
sample
method on an array will return a random item from the array- using it with
group.member_ids
will give us one of the ids from our group
- using it with
-
We use that id to re-fetch the computer object
-
then we look at the value of the EA