企業資料處理應用程式 (例如 Integration Services 封裝) 通常需要根據儲存在 Active Directory 中的職等、工作職稱或是員工的其他特色,以不同的方式處理資料。 Active Directory 是一種 Microsoft Windows 目錄服務,可集中儲存中繼資料,這些資料不僅有關使用者,而且還有關電腦與印表機等其他組織資產。
System.DirectoryServices Microsoft .NET Framework 中的命名空間提供使用 Active Directory 的類別,以協助您根據儲存的信息來引導數據處理工作流程。
注意
如果您想要建立可更輕鬆地在多個封裝之間重複使用的工作,請考慮使用此指令碼工作範例中的程式碼做為自訂工作的起點。 如需詳細資訊,請參閱 開發自訂工作。
描述
下列範例根據 email 變數值 (包含員工的電子郵件地址),從 Active Directory 擷取員工的姓名、職稱與電話號碼。 封裝中的優先順序條件約束可以使用擷取的資訊來判斷,例如,根據員工的工作職稱,來判斷要傳送低優先順序的電子郵件訊息或是高優先順序的頁面。
設定此指令碼工作範例
建立三個字串變數
email、name和title。 輸入有效的公司電子郵件地址做為email變數值。在 [腳本工作編輯器] 的 [腳本] 頁面上,將 變數新增
email至ReadOnlyVariables屬性。將
name和title變數新增至ReadWriteVariables屬性。在腳本專案中,新增命名空間的
System.DirectoryServices參考。. 在您的程式代碼中,使用
Imports語句匯入DirectoryServices命名空間。
注意
若要順利執行這個指令碼,您的公司必須在其網路上使用 Active Directory,並儲存這個範例所使用的員工資訊。
程式碼
Public Sub Main()
Dim directory As DirectoryServices.DirectorySearcher
Dim result As DirectoryServices.SearchResult
Dim email As String
email = Dts.Variables("email").Value.ToString
Try
directory = New _
DirectoryServices.DirectorySearcher("(mail=" & email & ")")
result = directory.FindOne
Dts.Variables("name").Value = _
result.Properties("displayname").ToString
Dts.Variables("title").Value = _
result.Properties("title").ToString
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
Dts.Events.FireError(0, _
"Script Task Example", _
ex.Message & ControlChars.CrLf & ex.StackTrace, _
String.Empty, 0)
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
public void Main()
{
//
DirectorySearcher directory;
SearchResult result;
string email;
email = (string)Dts.Variables["email"].Value;
try
{
directory = new DirectorySearcher("(mail=" + email + ")");
result = directory.FindOne();
Dts.Variables["name"].Value = result.Properties["displayname"].ToString();
Dts.Variables["title"].Value = result.Properties["title"].ToString();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.Events.FireError(0, "Script Task Example", ex.Message + "\n" + ex.StackTrace, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
外部資源
- social.technet.microsoft.com 上的技術文件:Processing Active Directory Information in SSIS (在 SSIS 中處理 Active Directory 資訊)
使用 Integration Services 保持最新狀態
如需來自Microsoft的最新下載、文章、範例和影片,以及來自社群的所選解決方案,請流覽 MSDN 上的 Integration Services 頁面:
流覽 MSDN 上的 Integration Services 頁面
如需這些更新的自動通知,請訂閱頁面上可用的 RSS 摘要。