SSIS使用Script task處理Active Directory

SSIS的Script Task有着很大的自由度,可以通過寫代碼的方式來實現各種各樣的需求。

這裏以C#代碼爲例,對AD信息進行操作。


首先需要添加引用System.DirectoryServices

然後修改main方法即可進行下一步操作

	/// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
	public void Main()
	{
            try
            {
                //  get the directory entry root
                DirectoryEntry dirEntry = new DirectoryEntry();
                dirEntry.Path = "LDAP://CNSRVADC01/DC=CORP,DC=Intra";
                DirectoryEntries adUsers = dirEntry.Children;
                DirectoryEntry adUser;
                //  search users
                DirectorySearcher deSearch = new DirectorySearcher();
                deSearch.Filter = ("(&(objectClass=User) (objectCategory=Person))");
                deSearch.SearchRoot = dirEntry;
                SearchResultCollection results = deSearch.FindAll();

                for (int i = 0; i < results.Count; i++)
                {
                    adUser = new DirectoryEntry(results[i].Path);
                    //...add code here to handle the AD INFO 
                }

                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch (Exception ex)
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }
	}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章