Skip to content

Instantly share code, notes, and snippets.

@kwoods
Created September 18, 2012 13:09
Show Gist options
  • Save kwoods/3743005 to your computer and use it in GitHub Desktop.
Save kwoods/3743005 to your computer and use it in GitHub Desktop.
Search for Computer Names using List of Users
# This script will take a list of user names
# formatted like so: Amos, Deborah K
# and convert the name into LastnameFirstInitial
# to use for searching Active Directory for
# matching computer names...
require 'net-ldap'
@ldap = Net::LDAP.new :host => "ip-addr-of-dc",
:port => 389, :base=> "dc=my,dc=domain,dc=com",
:auth => {
:method => :simple,
:username => "username@my.domain.com",
:password => ""
}
def ad_search(full_name)
search_filter = full_name.split(",")[0] + full_name.split(",")[1].lstrip.chr
filter = Net::LDAP::Filter.eq("cn", search_filter + "*")
treebase = "dc=my, dc=domain, dc=com"
full_name = full_name.sub(/,/, " ").chomp
@ldap.search(:base => treebase, :filter => filter) do |entry|
puts "#{full_name}, #{search_filter}, #{entry.dn}"
end
end
users = IO.readlines("c:/exclusion_list.txt")
users.each do |full_name|
ad_search(full_name)
end
p ldap.get_operation_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment