Hi,
Following is my code and I am not sure why the userNAme is treated as char.Also,somehow the section of code below does not seem right from the point that I want to enumerate the table and select just the username column from the table and out it into list.Then first remove members from group and add them back from table.I am seeing the error converting char to string foruserName below:
foreach (var userName in result)
{
AddUserToGroup(context,group,userName);
}
Actual code:
public void Main()
{
try
{
var table = new DataTable();
var adapter = new OleDbDataAdapter();
adapter.Fill(table, Dts.Variables["User::UserList"].Value);
var results = from r in table.AsEnumerable()
select r.Field<string>("user_name").ToList();
var context = new PrincipalContext(ContextType.Domain, "xxx.xx.xx.xx");
foreach (var res in results)
{
var group = GroupPrincipal.FindByIdentity(context, "ABCD123");
group.Members.Clear();
foreach (var userName in result)
{
AddUserToGroup(context,group,userName);
}
group.Save();
}
}
catch (Exception exp)
{
//MessageBox.Show(ex.ToString());
var msg = String.Format("{0} - {1}", exp.Message, Environment.NewLine);
System.IO.File.AppendAllText(@"C:\Users\Tdam\Documents\ADError.txt", "Date :" +
DateTime.Now.ToString() + "\t" +userName + "\t" + "Message:" + msg);
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void AddUserToGroup(PrincipalContext context,GroupPrincipal group, string userName)
{
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName);
if (!group.Members.Contains(user))
group.Members.Add(user);
}
}
Also,pls let me know if i am proceeding in right direction in terms of searching users in context and then adding/removing users from a specific group.