using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.Management;
using System.Threading;

namespace LocalGroup_AD
{
    class Program
    {
        static void Main(string[] args)
        {
            string r = null;
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
            {
                ManagementObjectCollection information = searcher.Get();
                if (information != null)
                {
                    foreach (ManagementObject obj in information)
                    {
                        r = obj["ProductType"].ToString();
                    }
                }

                Console.WriteLine("ProductType: {0}", r);
            }

            string osType = null;
            switch (r)
            {
                case "1":  osType = "WKS"; break;
                case "3":  osType = "SRV"; break;
                default:
                    Console.WriteLine("No valid input for osType");
                    Environment.Exit(0);
                    break;
            }

            //Console.ReadKey();
            //Environment.Exit(0);

            if (args.Count() != 1)
            {
                Console.WriteLine("Usage: LocalGroup_AD.exe [adm|rdu]");
                Environment.Exit(0);
            }

            string PC = System.Environment.MachineName;
            string sid = null;

            string lGroup = null;
            string groupName = null;
            switch (args[0].ToLower())
            {
                case "adm": lGroup = "Administrators"; groupName = "local_administrator_" + PC; sid = "544";  break;
                case "rdu": lGroup = "RDU"; groupName = "local_rdu_" + PC; sid = "555";  break;
                default: 
                    Console.WriteLine("No valid input for groupName");  
                    Environment.Exit(0); 
                    break;
            }

            DirectoryEntry dom = new DirectoryEntry();

            string pathDN = "OU=" + lGroup + ",OU=" + osType + ",OU=LocalGroups,OU=Security Groups";

            Console.WriteLine(pathDN);
            DirectoryEntry ou = dom.Children.Find(pathDN);

            bool groupName_exist = false;
            try
            {
                DirectoryEntry childGroup = ou.Children.Find("CN=" + groupName);
                if (childGroup != null)
                    groupName_exist = true;
            }
            catch { }

            if (groupName_exist)
            {
                Console.WriteLine("Group {0} exist", groupName);
            }
            else
            {
                try
                {
                    DirectoryEntry group = ou.Children.Add("CN=" + groupName, "group");
                    group.Properties["samAccountName"].Value = groupName;


                    group.CommitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Try add group to domain..." + Environment.NewLine + e.ToString());
                }
            }




            ManagementObjectSearcher searchGroup = new ManagementObjectSearcher(@"SELECT name FROM Win32_Group where LocalAccount = true and sid = 'S-1-5-32-"+ sid +"'");
            ManagementObjectCollection adminGroup = searchGroup.Get();
            string gr = null;

            foreach (ManagementObject group in adminGroup)
            {
                gr = group["Name"].ToString();
                Console.WriteLine(group["Name"].ToString());
                continue;
            }

            Thread.Sleep(10000);

            DirectoryEntry localGroup = new DirectoryEntry(String.Format("WinNT://{0}/{1},group", Environment.MachineName, gr));
            DirectoryEntry remoteGroup = new DirectoryEntry(String.Format("WinNT://{0}/{1}", Domain, groupName));
            try
            {
                localGroup.Invoke("Add", new object[] { remoteGroup.Path });
                localGroup.CommitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine("Try add group to local group..." + Environment.NewLine + e.ToString());
            }

        }
    }
}