Пример #1
0
        private void getSharedFolder()
        {
            //'System.IO.IOException'
            try
            {
                UserImpersonation impersonator = new UserImpersonation();
                impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required

                //string folderPath = ProgParams.FolderPath.Substring(0, 18);
               // DirectoryInfo directory = new DirectoryInfo(ProgParams.FolderPath);

                ShareCollection shi = ShareCollection.GetShares(ProgParams.FolderPath);
                if (shi != null)
                {
                    foreach (Share si in shi)
                    {
                        Console.WriteLine("{0}: {1} [{2}]",
                            si.ShareType, si, si.Path);

                        DirectoryInfo d = new DirectoryInfo(si.ToString());
                        if (d.Name.Contains("es"))
                        {
                            folders.Add(d);
                        }
                        // If this is a file-system share, try to
                        // list the first five subfolders.
                        // NB: If the share is on a removable device,
                        // you could get "Not ready" or "Access denied"
                        // exceptions.
                        //if (si.IsFileSystem)
                        //{
                        //    try
                        //    {
                        //        System.IO.DirectoryInfo d = si.Root;
                        //        System.IO.DirectoryInfo[] Flds = d.GetDirectories();
                        //        for (int i = 0; i < Flds.Length && i < 5; i++)
                        //            Console.WriteLine("\t{0} - {1}", i, Flds[i].FullName);

                        //        Console.WriteLine();
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        Console.WriteLine("\tError listing {0}:\n\t{1}\n",
                        //            si, ex.Message);
                        //    }
                        //}
                    }
                }

                //folders = directory.Parent.GetDirectories();
                impersonator.undoimpersonateUser();
            }
            catch (IOException)
            {
                MessageBox.Show("Could not receive the shared folders from server. Please make sure server is accessible from from this computer.");
            }
        }
Пример #2
0
        private void moveFolderToBackup(ADUsers user)
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword);//No Domain is required
            try
            {
                //\\192.168.0.111\backups
                string dest = ProgParams.BackupFolder + @"\" + user.Username;
                System.IO.Directory.CreateDirectory(dest);
                System.IO.Directory.CreateDirectory(dest + "\\HDrive");
                System.IO.Directory.CreateDirectory(dest + "\\Profile");
                System.IO.Directory.CreateDirectory(dest + "\\ProfileV2");
                //System.IO.Directory.Move(user.HDrive, dest);

                //System.IO.Directory.CreateDirectory(destDirName);

                DirectoryInfo sourceDir = new DirectoryInfo(user.HDrive);
                DirectoryInfo destDir = new DirectoryInfo(dest + "\\HDrive");

                //Copy the files in the current directory.
                FileInfo[] files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\HDrive", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                DirectoryInfo[] subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\HDrive", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.HDrive, true);

                sourceDir = new DirectoryInfo(user.ProfilePath);
                destDir = new DirectoryInfo(dest + "\\Profile");
                //Copy the files in the current directory.
                files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\Profile", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\Profile", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.ProfilePath, true);

                sourceDir = new DirectoryInfo(user.ProfilePath + ".V2");
                destDir = new DirectoryInfo(dest + "\\ProfileV2");

                //Copy the files in the current directory.
                files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\ProfileV2", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\ProfileV2", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.ProfilePath + ".V2", true);

                //System.IO.Directory.Move(user.ProfilePath, dest);
                //System.IO.Directory.Move(user.ProfilePath + ".V2", dest);
            }
            catch (Exception) { }
            impersonator.undoimpersonateUser();
        }
Пример #3
0
        private void backupOldUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required
            foreach (DataGridViewRow row in grdUsers.Rows)
            {
                ADUsers user = (ADUsers)row.DataBoundItem;
                //string readableLastLogon = DateTime.FromFileTime(Convert.ToInt64(user.LastLogin)).ToString();
                if (user != null)
                {
                    int months = Math.Abs((DateTime.Now.Month - user.LastLogin.Month) + 12 * (DateTime.Now.Year - user.LastLogin.Year));
                    if (months >= 18 && months <= 4000)
                    {
                        if (user.ExistsInActiveDirectory)
                        {
                            if (user.ProfilePath != null && user.HDrive != null)
                            {
                                string DomainPath = "LDAP:// " + ProgParams.ServerConnectionString;
                                DirectoryEntry searchRoot = new DirectoryEntry(DomainPath, ProgParams.UserAccount, ProgParams.UserPassword);
                                DirectorySearcher search = new DirectorySearcher(searchRoot);

                                search.Filter = "(&(objectClass=user)(objectCategory=person)(CN=" + user.Username + "))";
                                SearchResult result;
                                SearchResultCollection resultCol = search.FindAll();
                                if (resultCol != null)
                                {
                                    for (int counter = 0; counter < resultCol.Count; counter++)
                                    {
                                        string UserNameEmailString = string.Empty;
                                        result = resultCol[counter];
                                        if (result.Properties.Contains("samaccountname") && result.Properties.Contains("mail") && result.Path.Contains("students"))
                                        {
                                            string parentOU = user.Path.Replace("CN=" + user.Username + ",", "");
                                            DirectoryEntry de = new DirectoryEntry(parentOU);
                                            de.Children.Remove(result.GetDirectoryEntry());
                                            searchRoot.CommitChanges();
                                            moveFolderToBackup(user);
                                            Users.Remove(user);

                                        }
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Please set OU group for user");
                            }

                        }
                    }
                }
            }
            grdUsers.Refresh();
            impersonator.undoimpersonateUser();
        }
Пример #4
0
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ADUsers user = (ADUsers)grdUsers.Rows[mouseLocation.RowIndex].DataBoundItem;
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required
            if (user.ExistsInActiveDirectory)
            {
                if (user.ProfilePath != null && user.HDrive != null)
                {
                    string DomainPath = "LDAP:// " + ProgParams.ServerConnectionString;
                    DirectoryEntry searchRoot = new DirectoryEntry(DomainPath, ProgParams.UserAccount, ProgParams.UserPassword);
                    DirectorySearcher search = new DirectorySearcher(searchRoot);

                    search.Filter = "(&(objectClass=user)(objectCategory=person)(CN=" + user.Username + "))";
                    SearchResult result;
                    SearchResultCollection resultCol = search.FindAll();
                    if (resultCol != null)
                    {
                        for (int counter = 0; counter < resultCol.Count; counter++)
                        {
                            string UserNameEmailString = string.Empty;
                            result = resultCol[counter];
                            if (result != null)
                            {
                                if (result.Properties.Contains("samaccountname") && result.Properties.Contains("mail") && result.Path.Contains("students"))
                                {
                                    string parentOU = user.Path.Replace("CN=" + user.Username + ",", "");
                                    DirectoryEntry de = new DirectoryEntry(parentOU);
                                    de.Children.Remove(result.GetDirectoryEntry());
                                    searchRoot.CommitChanges();
                                    moveFolderToBackup(user);
                                    Users.Remove(user);
                                    grdUsers.Refresh();
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please set OU group for user");
                }
            }
            impersonator.undoimpersonateUser();
        }
Пример #5
0
        private void AddUsersAsync()
        {
            try
            {
                UserImpersonation impersonator = new UserImpersonation();
                impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required
                foreach (DataGridViewRow row in grdUsers.SelectedRows)
                {

                    ADUsers user = (ADUsers)row.DataBoundItem;
                    if (!user.ExistsInActiveDirectory)
                    {
                        if (user.ProfilePath != null && user.HDrive != null)
                        {
                            // set up domain context

                            // create a user principal object

                            DirectoryEntry dirEntry = new DirectoryEntry(user.Path);
                            DirectoryEntry newUser = dirEntry.Children.Add("CN=" + user.Username, "user");
                            newUser.Properties["samAccountName"].Value = user.Username;
                            newUser.Properties["userprincipalname"].Value = user.Username + "@eng.western";
                            newUser.Properties["mail"].Value = user.Email;
                            newUser.Properties["givenname"].Value = user.FirstName + " " + user.MiddleName;
                            newUser.Properties["sn"].Value = user.LastName;
                            //newUser.Properties["initials"].Value = user.MiddleName;
                            newUser.Properties["profilepath"].Value = user.ProfilePath;
                            newUser.Properties["homedirectory"].Value = user.HDrive;
                            newUser.Properties["homeDrive"].Value = "H:";
                            newUser.Properties["displayname"].Value = user.FirstName + " " + user.MiddleName + " " + user.LastName;
                            //newUser.Properties["name"].Value = user.FirstName + " " + user.MiddleName + " " + user.LastName;
                            newUser.Properties["scriptPath"].Value = "login.bat";

                            newUser.CommitChanges();
                            string oGUID = newUser.Guid.ToString();

                            newUser.Invoke("SetPassword", new object[] { user.Password });
                            newUser.Properties["pwdLastSet"].Value = "0";
                            newUser.Properties["displayname"].Value = user.FirstName + " " + user.LastName;
                            int val = (int)newUser.Properties["userAccountControl"].Value;
                            newUser.Properties["userAccountControl"].Value = val & ~0x2; //enable account
                            //newUser.Properties["memberOf"].Add("students");
                            newUser.CommitChanges();

                            //foreach (string value in StudentsGroup.Properties.PropertyNames)
                            //{
                            //    // Do something with the value
                            //    Console.WriteLine(value);

                            //}
                            //StudentsGroup.Properties["member"].Add(newUser.Path);

                            dirEntry.Close();
                            newUser.Close();
                            user.Sid = (byte[])newUser.Properties["objectSid"][0];
                            //DirectoryEntry StudentsGroup = new DirectoryEntry("LDAP://" + @"ebithp-d2a/OU=students,DC=eng,DC=western");
                            //StudentsGroup.Invoke("Member", new object[] { newUser.Path });
                            //StudentsGroup.CommitChanges();
                            //StudentsGroup.Close();
                            user.ExistsInActiveDirectory = true;
                            AddToGroup(user.Username);
                            SetFolders(user);

                            row.Selected = false;

                        }
                        else
                        {
                            MessageBox.Show("Please set OU group for user");
                        }
                    }

                }

                MessageBox.Show("Users added to active directory successfully.");
                impersonator.undoimpersonateUser();
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //DoSomethingwith --> E.Message.ToString();
                Console.WriteLine(E.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #6
0
        private void addToADToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ADUsers user = (ADUsers)grdUsers.Rows[mouseLocation.RowIndex].DataBoundItem;
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required
            if (!user.ExistsInActiveDirectory)
            {
                if (user.ProfilePath != null && user.HDrive != null)
                {
                    DirectoryEntry dirEntry = new DirectoryEntry(user.Path);
                    DirectoryEntry newUser = dirEntry.Children.Add("CN=" + user.Username, "user");
                    newUser.Properties["samAccountName"].Value = user.Username;
                    newUser.Properties["userprincipalname"].Value = user.Username + "@eng.western";
                    newUser.Properties["mail"].Value = user.Email;
                    newUser.Properties["givenname"].Value = user.FirstName;
                    newUser.Properties["sn"].Value = user.LastName;
                    newUser.Properties["initials"].Value = user.MiddleName;
                    newUser.Properties["profilepath"].Value = user.ProfilePath;
                    newUser.Properties["homedirectory"].Value = user.HDrive;
                    newUser.Properties["homeDrive"].Value = "H:";
                    newUser.Properties["displayname"].Value = user.FirstName + " " + user.LastName;
                    //newUser.Properties["name"].Value = user.FirstName + " " + user.MiddleName + " " + user.LastName;
                    newUser.Properties["scriptPath"].Value = "login.bat";

                    newUser.CommitChanges();
                    string oGUID = newUser.Guid.ToString();

                    newUser.Invoke("SetPassword", new object[] { user.Password });
                    newUser.Properties["pwdLastSet"].Value = "0";
                    newUser.Properties["displayname"].Value = user.FirstName + " " + user.LastName;
                    int val = (int)newUser.Properties["userAccountControl"].Value;
                    newUser.Properties["userAccountControl"].Value = val & ~0x2; //enable account
                    newUser.CommitChanges();
                    dirEntry.Close();
                    newUser.Close();
                    user.Sid = (byte[])newUser.Properties["objectSid"][0];
                    user.ExistsInActiveDirectory = true;
                    SetFolders(user);

                }
                else
                {
                    MessageBox.Show("Please set OU group for user");
                }
            }
            impersonator.undoimpersonateUser();
        }
Пример #7
0
        private void addPsHs()
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required

            DirectoryInfo directory = new DirectoryInfo(ProgParams.FolderPath);

            //folders = directory.GetDirectories();

            foreach (DirectoryInfo folder in folders)
            {
                string h = System.IO.Path.Combine(folder.FullName, "h");
                string p = System.IO.Path.Combine(folder.FullName, "p");
                System.IO.Directory.CreateDirectory(p);
                System.IO.Directory.CreateDirectory(h);
            }
            impersonator.undoimpersonateUser();
        }
Пример #8
0
        private void AddAllUsersAsync()
        {
            try
            {
                UserImpersonation impersonator = new UserImpersonation();
                impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required
                foreach (DataGridViewRow row in grdUsers.Rows)
                {

                    ADUsers user = (ADUsers)row.DataBoundItem;
                    if (user != null)
                    {
                        if (!user.ExistsInActiveDirectory)
                        {
                            if (user.ProfilePath != null && user.HDrive != null)
                            {
                                DirectoryEntry dirEntry = new DirectoryEntry(user.Path);
                                DirectoryEntry newUser = dirEntry.Children.Add("CN=" + user.Username, "user");
                                newUser.Properties["samAccountName"].Value = user.Username;
                                newUser.Properties["userprincipalname"].Value = user.Username + "@eng.western";
                                newUser.Properties["mail"].Value = user.Email;
                                newUser.Properties["givenname"].Value = user.FirstName + " " + user.MiddleName;
                                newUser.Properties["sn"].Value = user.LastName;
                                //newUser.Properties["initials"].Value = user.MiddleName;
                                newUser.Properties["profilepath"].Value = user.ProfilePath;
                                newUser.Properties["homedirectory"].Value = user.HDrive;
                                newUser.Properties["homeDrive"].Value = "H:";
                                newUser.Properties["displayname"].Value = user.FirstName + " " + user.MiddleName + " " + user.LastName;
                                //newUser.Properties["name"].Value = user.FirstName + " " + user.MiddleName + " " + user.LastName;
                                newUser.Properties["scriptPath"].Value = "login.bat";

                                newUser.CommitChanges();
                                string oGUID = newUser.Guid.ToString();

                                newUser.Invoke("SetPassword", new object[] { user.Password });
                                newUser.Properties["pwdLastSet"].Value = "0";
                                newUser.Properties["displayname"].Value = user.FirstName + " " + user.LastName;
                                int val = (int)newUser.Properties["userAccountControl"].Value;
                                newUser.Properties["userAccountControl"].Value = val & ~0x2; //enable account
                                newUser.CommitChanges();
                                dirEntry.Close();
                                newUser.Close();
                                user.Sid = (byte[])newUser.Properties["objectSid"][0];
                                user.ExistsInActiveDirectory = true;
                                SetFolders(user);

                                row.Selected = false;
                            }
                            else
                            {
                                MessageBox.Show("Please set OU group for user");
                            }
                        }
                    }

                }

                MessageBox.Show("Users added to active directory successfully.");
                impersonator.undoimpersonateUser();
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //DoSomethingwith --> E.Message.ToString();

            }
        }
Пример #9
0
        private void SetFolders(ADUsers user)
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required

            System.IO.Directory.CreateDirectory(user.HDrive);

            if (System.IO.Directory.Exists(user.HDrive))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.HDrive);
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            System.IO.Directory.CreateDirectory(user.ProfilePath);
            if (System.IO.Directory.Exists(user.ProfilePath))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.ProfilePath);
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            System.IO.Directory.CreateDirectory(user.ProfilePath + ".V2");
            if (System.IO.Directory.Exists(user.ProfilePath + ".V2"))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.ProfilePath + ".V2");
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            impersonator.undoimpersonateUser();
        }