コード例 #1
0
        public string ADBulkImport()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            ParksEmployeeEntities db = new ParksEmployeeEntities();

            db.uspDeleteADUser();
            int cntUsers = 0;

            using (db)
            {
                ADManager am   = new ADManager();
                var       test = am.Groups();
                foreach (var itemGroup in test)
                {
                    var name = itemGroup.ToString();

                    var users = am.Users(name);

                    foreach (var itemUser in users)
                    {
                        if (itemUser.StructuralObjectClass != null && itemUser.StructuralObjectClass.ToUpper() == "USER")
                        {
                            var userData = new ADUser();

                            userData.GUID = itemUser.Guid.ToString();

                            if (((System.DirectoryServices.AccountManagement.UserPrincipal)(itemUser)).EmailAddress == null)
                            {
                                userData.Email = "";
                            }
                            else
                            {
                                userData.Email = ((System.DirectoryServices.AccountManagement.UserPrincipal)(itemUser)).EmailAddress.ToString();
                            }



                            if (((System.DirectoryServices.AccountManagement.UserPrincipal)(itemUser)).VoiceTelephoneNumber == null)
                            {
                                userData.PhoneNumber = "";
                            }
                            else
                            {
                                userData.PhoneNumber = ((System.DirectoryServices.AccountManagement.UserPrincipal)(itemUser)).VoiceTelephoneNumber.ToString();
                            }

                            userData.UserName = itemUser.SamAccountName.ToString();
                            if (itemUser.DisplayName != null)
                            {
                                userData.DisplayName = itemUser.DisplayName.ToString();
                            }
                            else
                            {
                                userData.DisplayName = "";
                            }

                            userData.GroupName = name;

                            try
                            {
                                db.ADUsers.Add(userData);


                                db.SaveChanges();
                                cntUsers++;
                            }

                            catch (DbEntityValidationException dbEx)
                            {
                                foreach (var validationErrors in dbEx.EntityValidationErrors)
                                {
                                    foreach (var validationError in validationErrors.ValidationErrors)
                                    {
                                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                               validationError.PropertyName,
                                                               validationError.ErrorMessage);
                                    }
                                }
                            }


                            finally
                            {
                            }
                        }
                    }
                }
            }
            stopWatch.Stop();
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);


            return(cntUsers.ToString() + " Users are imported DB during " + elapsedTime);
        }