-
-
Save jamesgecko/1705166 to your computer and use it in GitHub Desktop.
ironruby ActiveDirectory access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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