M
msdnpublic1234
Guest
I have below code and trying to add users to Ad group as part of a script task in SSIS.The script is inside a foreach loop container which will iterate over each row from query,pick the users and add them into the AD group iteratively.
The issue is my Ad group LDAP structure is such that I have to find users from domain(abc.com)->Users and add that user to another group under a different location "Group" residing in (reads top to bottom) abc.com->NA->US->XYZ->Distr.List->Group
I am seeing lot of exceptions and error messages,unable to understand what.I think,my context is wrong.Pls guide.
using System;
using System.DirectoryServices.AccountManagement;
using System.Windows.Forms;
namespace ST_a66e3c28bec84b718f6afa04448e0bec
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void Main()
{
/* TODO: Add your code here
PrincipalContext ouContex = new
PrincipalContext(ContextType.Domain
, "abc.com", "CN=123Group,OU=Distr.List,OU=XYZ,OU=US,OU=NA,DC=itron,DC=com");
*/
try
{
string firstName = Dts.Variables["User::Firstname"].Value.ToString();
string lastName = Dts.Variables["User::Lastname"].Value.ToString();
string name = firstName +","+ lastName;
string group = Dts.Variables["User::Group"].Value.ToString();
AddUserToGroup(name, group);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void AddUserToGroup(string userId, string groupName)
{
try
{
using (PrincipalContext pc = new
PrincipalContext(ContextType.Domain,
"itron.com", "OU=Users,DC=abc,DC=com"))
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
group.Members.Add(pc, IdentityType.Name, userId);
group.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Continue reading...
The issue is my Ad group LDAP structure is such that I have to find users from domain(abc.com)->Users and add that user to another group under a different location "Group" residing in (reads top to bottom) abc.com->NA->US->XYZ->Distr.List->Group
I am seeing lot of exceptions and error messages,unable to understand what.I think,my context is wrong.Pls guide.
using System;
using System.DirectoryServices.AccountManagement;
using System.Windows.Forms;
namespace ST_a66e3c28bec84b718f6afa04448e0bec
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void Main()
{
/* TODO: Add your code here
PrincipalContext ouContex = new
PrincipalContext(ContextType.Domain
, "abc.com", "CN=123Group,OU=Distr.List,OU=XYZ,OU=US,OU=NA,DC=itron,DC=com");
*/
try
{
string firstName = Dts.Variables["User::Firstname"].Value.ToString();
string lastName = Dts.Variables["User::Lastname"].Value.ToString();
string name = firstName +","+ lastName;
string group = Dts.Variables["User::Group"].Value.ToString();
AddUserToGroup(name, group);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void AddUserToGroup(string userId, string groupName)
{
try
{
using (PrincipalContext pc = new
PrincipalContext(ContextType.Domain,
"itron.com", "OU=Users,DC=abc,DC=com"))
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
group.Members.Add(pc, IdentityType.Name, userId);
group.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Continue reading...