Skip to content

JNUC2019 Lab Session K Scripts

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

Saving a Scripted Task

  • while its great to have a live, interactive shell to do things in your JSS like this, you'll also probably want to write scripts to be run as needed, either manually or with launchd or cron

  • Lets write a little script that updates a Static Computer Group and a Static MobileDevice Group, based on the contents of a Smart User Group

  • For example, say you have a User Extension Attribute that marks users as VIPs, and you have a smart group of those users

  • You can't create Computer or Device smart groups based on a User extension attribute - so here's how to automatically maintain computer and devices groups for those users

  • To make this work, we'll have to all create or own computer and device groups, so when we update them, we don't stomp on each other

  • Think up a couple of unique names for your two groups and create them now:

JSS::ComputerGroup.make(name: 'ChrissVIP-Computers', type: :static).save ;0
# => 0

JSS::MobileDeviceGroup.make(name: 'ChrissVIP-Devices', type: :static).save ;0
# => 0
#!/usr/bin/ruby

require 'ruby-jss'

COMPUTER_GROUP = 'YOUR GROUP NAME HERE'
DEVICE_GROUP = 'YOUR GROUP NAME HERE'

JSS.api.connect server: 'tryitout.jamfcloud.com', user: 'jnuc2019', pw: :prompt

# Smart group based on User Ext Attrib 'vip' containing 'yes'
user_group = JSS::UserGroup.fetch name: 'VIPs'

# Static Groups
comp_group = JSS::ComputerGroup.fetch name: COMPUTER_GROUP
dev_group = JSS::MobileDeviceGroup.fetch name: DEVICE_GROUP

# Clear out the group memberships in the static groups
comp_group.clear
dev_group.clear

# loop thru the user ids in the user group
user_group.member_ids.each do |user_id|

  # fetch the user object
  user = JSS::User.fetch id: user_id

  puts "----- Processing user: #{user.name}"

  # loop thru their computers, adding to the computer group
  user.computers.each do |comp|
    comp_group.add_member comp[:id]
    puts "  ..Added Computer '#{comp[:name]}''"
  end # user.computers.each

  # loop thru their devices, adding to the device group
  user.mobile_devices.each do |dev|
    dev_group.add_member dev[:id]
    puts "  ..Added Device '#{dev[:name]}'"
  end # user.mobile_devices

end # user_group.member_ids.each

puts '----- Done with users'

# add_member doesn't save immediately like change_membership
# so we need to save the group changes
comp_group.save
puts '----- Saved Computer Group'

dev_group.save
puts '----- Saved Mobile Device Group'

puts 'All Done!'
  • Open your preferred text editor, and save this code into a file named 'update-vip-hardware-groups'

  • make the file executable, chmod 755 update-vip-hardware-groups

  • Now you can run it by name ./update-vip-hardware-groups

  • You'll be prompted for the password, and off it goes

  • In the real world, this script would be more robust, for example, catching errors and reporting them properly, but for now this will do for

Advanced ruby-jss

Documentation and Resources

Clone this wiki locally