Skip to content

Instantly share code, notes, and snippets.

@mdnmdn
Created December 19, 2011 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdnmdn/1496223 to your computer and use it in GitHub Desktop.
Save mdnmdn/1496223 to your computer and use it in GitHub Desktop.
ironruby ActiveDirectory access
require 'System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
include System::DirectoryServices
class WinManager
attr_accessor :connection_string
if !defined? FLAG_CANNOT_CHANGE_PASSWORD
FLAG_CANNOT_CHANGE_PASSWORD = 0x40
FLAG_PASSWORD_NEVER_EXPIRES = 0x10000
end
def initialize
@connection_string = 'WinNT://localhost'
end
def create_user username,full_name,password
begin
unless user_exists? username
entries = entry_point.Children
user = entries.Add username,'User'
user.Properties['FullName'].Add full_name.to_clr_string
user.Properties['Userflags'].Add FLAG_CANNOT_CHANGE_PASSWORD | FLAG_PASSWORD_NEVER_EXPIRES
user.Invoke 'SetPassword',password.to_clr_string
user.CommitChanges
user.Close
end
rescue
puts 'cannot create ' + username
return false
end
true
end
def user_exists? username
begin
u = entry_point.Children.find username,'User'
u.close
rescue
return false
end
true
end
def entry_point
@main_entry ||= DirectoryEntry.new @connection_string.to_clr_string
end
end
#winMgr = WinManager.new
#winMgr.create_user 'pippo','Pippo','pippopippo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment