using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.Diagnostics;
using System.IO;
namespace CreateQSPRUserGroups
{
class Program
{
private string QTM_Directory = @"C:\Program Files\QUALCOMM\QDART\bin";
private const string SUBKEY = "Path";
private const string QSPR_POWER_USERS = "QSPR Power Users";
private const string QSPR_GROUP_DESCRIPTION = "QSPR Power Users have Engineer Privileges to run QSPR";
private const string QSPR_TECH_USERS = "QSPR Tech Users";
private const string QSPR_TECH_GROUP_DESCRIPTION = "QSPR Tech Users have Technician Privileges to run QSPR";
static void Main(string[] args)
{
//write to a log file also all the output messages for debugging later
using (FileStream fs = new FileStream("CreateGroups.log", FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
Console.SetOut(sw);
CreateUserGroup(QSPR_POWER_USERS, QSPR_GROUP_DESCRIPTION);
CreateUserGroup(QSPR_TECH_USERS, QSPR_TECH_GROUP_DESCRIPTION);
AddAdminUsersToGroup();
sw.Close();
}
}
}
private static void CreateUserGroup(string groupName, string groupDescription)
{
try
{
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Starting to add group " + groupName);
DirectoryEntry entry = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Found the NT directory.");
DirectoryEntry qtmGroup = entry.Children.Add(groupName, "group");
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Setup the group");
qtmGroup.Invoke("Put", new object[] { "Description", groupDescription });
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Setup the group's description");
qtmGroup.CommitChanges();
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + groupName + " added");
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + string.Format("Failed to create user group {0}, Error: {1}, InnerException: {2}",
groupName, ex.Message, ex.InnerException));
}
}
private static void AddAdminUsersToGroup()
{
try
{
DirectoryEntry AD3 = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
DirectoryEntry grp;
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Finding the QSPR Power users");
grp = AD3.Children.Find(QSPR_POWER_USERS, "group");
if (grp != null)
{
using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
{
foreach (object member in (System.Collections.IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
try
{
grp.Invoke("Add", new object[] { memberEntry.Path.ToString() });
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Adding user " + memberEntry.ToString());
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "User already added." + ex.Message);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + string.Format("Add admin users failed, Error: {0}, InnerException: {1}", ex.Message, ex.InnerException));
}
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment