使用脚本任务查询 Active Directory

适用于:SQL Server Azure 数据工厂中的 SSIS Integration Runtime

企业数据处理应用程序(如 Integration Services 包)通常需要根据 Active Directory 中存储的雇员的级别、职务或其他特征来以不同方式处理数据。 Active Directory 是一个 Microsoft Windows 目录服务,它不仅提供集中存储有关用户的元数据,还可以提供集中存储有关其他组织资产(如计算机和打印机)的元数据。 Microsoft .NET Framework 中的 System.DirectoryServices 命名空间提供使用 Active Directory 的类,以帮助根据 Active Directory 中存储的信息来定向数据处理工作流。

注意

如果希望创建可更方便地重用于多个包的任务,请考虑以此脚本任务示例中的代码为基础,创建自定义任务。 有关详细信息,请参阅 开发自定义任务

说明

下面的示例基于 email 变量的值(包含雇员的电子邮件地址),从 Active Directory 检索雇员的姓名、职务和电话号码。 包中的优先约束可使用检索到的信息,例如基于雇员的职务来确定发送具有低优先级的电子邮件消息,还是具有高优先级的页。

配置此脚本任务示例

  1. 创建三个字符串变量:emailnametitle。 输入一个有效的企业电子邮件地址作为 email 变量的值。

  2. 在“脚本任务编辑器”的“脚本”页,将 email 变量添加到 ReadOnlyVariables 属性中

  3. nametitle 变量添加到 ReadWriteVariables 属性中。

  4. 在脚本项目中,添加对 System.DirectoryServices 命名空间的引用。

  5. 。 在代码中,使用 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;  
    }  
  
}  

外部资源