コード例 #1
0
        public void GetDisabledUsers()
        {
            String       Arq    = "Inativos_" + DateTime.Now.ToString("dd.MM.yyyy") + ".csv";
            StreamWriter writer = new StreamWriter(@Arq, true);

            writer.WriteLine("Nome" + ";" + "Login" + ";" + "CPF");

            ds.Filter      = "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))";
            ds.SearchScope = SearchScope.Subtree;
            SearchResultCollection DisabledUsers = ds.FindAll();

            FrmProgressBar FrmProgressBar = new FrmProgressBar();

            FrmProgressBar.Show();

            int i = 0;
            int progress;
            int total = DisabledUsers.Count;

            try {
                foreach (SearchResult DisabledUser in DisabledUsers)
                {
                    LimparAtributos();

                    if (DisabledUser.GetDirectoryEntry().Properties["DisplayName"].Value != null)
                    {
                        User.Nome = DisabledUser.GetDirectoryEntry().Properties["Name"].Value.ToString();
                    }

                    if (DisabledUser.GetDirectoryEntry().Properties["SamAccountName"].Value != null)
                    {
                        User.Login = DisabledUser.GetDirectoryEntry().Properties["SamAccountName"].Value.ToString();
                    }

                    if (DisabledUser.GetDirectoryEntry().Properties["EmployeeNumber"].Value != null)
                    {
                        User.Cpf = DisabledUser.GetDirectoryEntry().Properties["EmployeeNumber"].Value.ToString();
                    }

                    writer.WriteLine(User.Nome + ";" + User.Login + ";" + User.Cpf);

                    i++;
                    progress = (i / total * 100);
                    FrmProgressBar.progressBar1.Value = progress;
                }
            }
            catch (Exception e) {
                MessageBox.Show(e.Message.ToString(), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            finally {
                writer.Close();
            }
        }
コード例 #2
0
        public void GetUsersByGroup(String[] grupos)
        {
            foreach (string grupo in grupos)
            {
                String       Arq    = grupo + "_" + DateTime.Now.ToString("dd.MM.yyyy") + ".csv";
                StreamWriter writer = new StreamWriter(@Arq, true);
                writer.WriteLine("Nome" + ";" + "Login" + ";" + "CPF" + ";" + "Status");

                ds.Filter      = "(&(objectCategory=group)(cn=" + grupo + "))";
                ds.SearchScope = SearchScope.Subtree;
                SearchResult grp     = ds.FindOne();
                string       dnGrupo = grp.GetDirectoryEntry().Properties["distinguishedname"].Value.ToString();

                try {
                    ds.Filter = "(&(memberOf=" + dnGrupo + ")(!userAccountControl:1.2.840.113556.1.4.803:=2))";
                    SearchResultCollection EnabledUsers = ds.FindAll();

                    foreach (SearchResult Enabled in EnabledUsers)
                    {
                        LimparAtributos();

                        if (Enabled.GetDirectoryEntry().Properties["DisplayName"].Value != null)
                        {
                            User.Nome = Enabled.GetDirectoryEntry().Properties["Name"].Value.ToString();
                        }

                        if (Enabled.GetDirectoryEntry().Properties["SamAccountName"].Value != null)
                        {
                            User.Login = Enabled.GetDirectoryEntry().Properties["SamAccountName"].Value.ToString();
                        }

                        if (Enabled.GetDirectoryEntry().Properties["EmployeeNumber"].Value != null)
                        {
                            User.Cpf = Enabled.GetDirectoryEntry().Properties["EmployeeNumber"].Value.ToString();
                        }

                        writer.WriteLine(User.Nome + ";" + User.Login + ";" + User.Cpf + ";" + "Ativo");
                    }


                    ds.Filter = "(&(memberOf=" + dnGrupo + ")(userAccountControl:1.2.840.113556.1.4.803:=2))";
                    SearchResultCollection DisabledUsers = ds.FindAll();

                    foreach (SearchResult DisabledUser in DisabledUsers)
                    {
                        LimparAtributos();

                        if (DisabledUser.GetDirectoryEntry().Properties["DisplayName"].Value != null)
                        {
                            User.Nome = DisabledUser.GetDirectoryEntry().Properties["Name"].Value.ToString();
                        }

                        if (DisabledUser.GetDirectoryEntry().Properties["SamAccountName"].Value != null)
                        {
                            User.Login = DisabledUser.GetDirectoryEntry().Properties["SamAccountName"].Value.ToString();
                        }

                        if (DisabledUser.GetDirectoryEntry().Properties["EmployeeNumber"].Value != null)
                        {
                            User.Cpf = DisabledUser.GetDirectoryEntry().Properties["EmployeeNumber"].Value.ToString();
                        }

                        writer.WriteLine(User.Nome + ";" + User.Login + ";" + User.Cpf + ";" + "Inativo");
                    }
                }
                catch (Exception e) {
                    MessageBox.Show(e.Message.ToString(), "Erro Grupos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                finally {
                    writer.Close();
                }
            }
        }