Skip to content

Instantly share code, notes, and snippets.

@leeramsay
Created November 7, 2016 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeramsay/afdc12953dd605edbacf0fd32e9c85d8 to your computer and use it in GitHub Desktop.
Save leeramsay/afdc12953dd605edbacf0fd32e9c85d8 to your computer and use it in GitHub Desktop.

Forgive me whoever I borrowed this from, I couldn't credit you because I can't find your script online anymore!

#!/usr/bin/env ruby
ComputerName = `scutil --get ComputerName`.strip
ADGroupAttribute = 'dsAttrTypeNative:memberOf'
GroupAttributeSeparator = /\s*(?=CN=)/
PlistGroupKey = 'directory_groups'
ManagedInstallDir=`defaults read /Library/Preferences/ManagedInstalls ManagedInstallDir`.strip
PlistLocation = File.join( "#{ManagedInstallDir}", "Conditionalitems" )
unless ComputerName.empty?
# Read the LDAP attribute that lists which groups our computer object is a member of
groups_string = `dscl /Search read /Computers/#{ComputerName}$ #{ADGroupAttribute}`
puts groups_string
unless groups_string.empty?
# Split the listed groups into an array (and remove the ldap attribute prefix)
groups = groups_string.split(GroupAttributeSeparator).collect{|g| g if g.chomp(':') != ADGroupAttribute}.compact
# Shorten the group names from the distinguished name to just the group name
groups.collect!{|g| "'#{g[/([^,])*/].gsub("CN=", "").downcase}'" } # use a substitution to remove CN=, system ruby 1.8.7 regex doesn't support lookbehinds
if groups.length.zero?
puts "No directory groups to list. Skipping setting preferences"
else
my_routine = Proc.new { |str| str.upcase }
puts "The following directory groups will be written to the #{PlistGroupKey} key: #{groups.join(' ')}"
puts `defaults write \"#{PlistLocation}\" #{PlistGroupKey} -array #{groups.join(' ')}`
end
`plutil -convert xml1 \"#{PlistLocation}.plist\"`
`chmod 0644 \"#{PlistLocation}.plist\"`
end
else
puts "Could not obtain computer name!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment