SharePoint 2016与外部系统人员信息同步(一,Excel数据导入到AD)

来源:互联网 发布:8寸windows平板电脑 编辑:程序博客网 时间:2024/05/20 06:51

SharePoint 2016 AD 数据同步(一,数据导入)

    class Program    {        static void Main(string[] args)        {            string ldapPath = "LDAP://10.xxx.xxx.xxx/DC=xxxx,DC=com";            string userName = "Administrator";            string password = "Admin123";            //reference http://www.cnblogs.com/CSharpDevelopers/p/3634635.html            //测试查找OU            DirectoryEntry rootDirectoryEntry;            if (userName != string.Empty)            {                rootDirectoryEntry = new DirectoryEntry(ldapPath, userName, password);            }            else            {                rootDirectoryEntry = new DirectoryEntry(ldapPath);            }            DataTable dt = GetTableFromExcel("D:\\SPCode_dct\\SPPM\\ADusers.xlsx");            //设定OU名            string currentValue = "FromCode";            //先检查是否存在。            try {                 DirectoryEntry getOuName = rootDirectoryEntry.Children.Find("OU = "+currentValue);//判断OU是否存在                if (getOuName != null)                {                    return;                }                } catch( Exception ex)               {                }                      //foreach (DirectoryEntry getOuName1 in entry.Children)            //{            //    string ouName = getOuName1.Name;            //}            DirectoryEntry currentOuDirectoryEntry = rootDirectoryEntry.Children.Add("OU=" + currentValue, "organizationalUnit");            currentOuDirectoryEntry.Properties["name"].Add(currentValue);            currentOuDirectoryEntry.CommitChanges();            //检查用户是否存在            foreach (DataRow dr in dt.Rows)            {                string sAMAccountName = dr["用户名"].ToString();                string displayName = dr["显示名"].ToString();                string newUserDefaultPassword = dr["pwd"].ToString();                DirectorySearcher userDirectorySearcher = new DirectorySearcher(currentOuDirectoryEntry,                     string.Format(@"(&(cn={0})(objectCategory=person)(objectClass=user))", displayName), new[] { "adspath" }, SearchScope.OneLevel);                SearchResult searchResult = userDirectorySearcher.FindOne();                if (searchResult == null)                {                using (DirectoryEntry currentUserDirectoryEntry = currentOuDirectoryEntry.Children.Add("CN=" + displayName, "user"))                {                    currentUserDirectoryEntry.Properties["sAMAccountName"].Value = sAMAccountName;                    currentUserDirectoryEntry.Properties["userPrincipalName"].Value = string.Format(@"{0}@{1}", sAMAccountName, "spdev");                    currentUserDirectoryEntry.Properties["displayName"].Value = displayName;                    currentUserDirectoryEntry.CommitChanges();                    //currentUserDirectoryEntry.Properties["userAccountControl"].Value = userAccountControl;                    currentUserDirectoryEntry.Properties["pwdLastSet"].Value = 0;                    currentUserDirectoryEntry.Invoke("SetPassword", new object[] { newUserDefaultPassword });//初始密码,要注意密码策略例如123.com 是可以的。                                            currentUserDirectoryEntry.CommitChanges();                }                }            }        }              private static DataTable GetTableFromExcel(string fileName)        {            DataTable dataTable = new DataTable();            string connectionString = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source ={0};Extended Properties='Excel 12.0 Xml;HDR=YES'", fileName);            //当这里提示错误的时候请下载,AccessDatabaseEngine.exe            using (OleDbConnection oleDbConnection = new OleDbConnection(connectionString))            {                oleDbConnection.Open();                DataTable schemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });                string sheetName = schemaTable.Rows[0].Field<string>("TABLE_NAME");                string commandText = string.Format("select * from [{0}]", sheetName);                using (OleDbDataAdapter adapter = new OleDbDataAdapter(commandText, oleDbConnection))                {                    adapter.Fill(dataTable);                }            }            return dataTable;        }    }


原创粉丝点击