I don't generally write in C# so this is a bit of a struggle for me.
I am building a console app that will query a active directory security group then I need to ignore if the user exists, add them if they are new or remove them from the excel sheet if they are not in the security group.
I have managed to get the app to query AD I just need it to check the existing excel sheet and update, add or remove as necessary.
Any help would be really appreciated
The excel sheet has the following headings;
UserID | Fname | Lname | Accesslvl | Department
//--------------------------------START-------------------------------------------------------------
//Sync Users From AD Security Group - (STAFF - Paxton All Access)
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); // set up domain context
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "STAFF - Paxton All Access");// find the group in question
if (group != null)// if found....
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
//do whatever you need to do to those members
UserPrincipal theUser = p as UserPrincipal;
if (theUser != null)
{
if (theUser.Enabled != true) //If user account is disabled then
{
}
else //IF user account is enabled then
{
//Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
//VARIABLES
//fname = {ADFirstname}
//lname= {ADLastname}
//UserID = {ADusername}
//Accesslevel = "STAFF ALL"
//Department = "STAFF"
//------------ADD ROW TO EXCEL HERE-----------------------
//Search if user exists via UserID
//IF user exist then update fname, lname, AccessLevel or Department columns if needed or continue
//Else add user using variables stated above
}
}
}
}