【问题标题】:Using AzureAD (or Active Directory) to populate Employee Id column in CSV file使用 AzureAD(或 Active Directory)填充 CSV 文件中的员工 ID 列
【发布时间】:2022-12-16 11:40:28
【问题描述】:

我有一个包含 4 列的简单 CSV 文件。我正在尝试使用 OwnerEmail 列和 Azure AD(或本地 Active Directory)通过 PowerShell 填充员工 ID 列。

原来的:

Employee ID DepartmentNumber OwnerEmail Cost
074674 test@conso.com 4353.345
456246 tester@conso.com 3452.453

后:

Employee ID DepartmentNumber OwnerEmail Cost
435345 074674 test@conso.com 4353.345
546345 456246 tester@conso.com 3452.453

我只是将 Employee Id 列添加到以前不存在的 Csv 文件中。我一直没能找到任何东西。一点建议和指导会很有帮助。我是 PowerShell 新手

我如何添加员工 ID 列:

$CSVI导入|选择对象 "employeeID",*

【问题讨论】:

    标签: powershell csv azure-active-directory active-directory system-administration


    【解决方案1】:

    在线搜索答案时,首先将任务拆分成更小的任务,然后搜索这些单独的任务。

    您已经知道如何向集合中添加新列,所以现在您需要:

    1. 遍历集合中的项目
    2. 通过电子邮件地址获取AD帐户并读取employeeID
    3. 将集合导出为 CSV

      如果您搜索“powershell”以及其中任何一个,您会找到很多答案。

      但我会帮助您完成第 1 步和第 2 步,它们看起来像这样(用于从本地 AD 读取):

      $employees = $CSVImport | Select-Object "employeeID",*
      
      foreach ($employee in $employees) {
          $user = Get-ADUser -Filter "EmailAddress -eq '$($employee.OwnerEmail)'" -Properties employeeID
          $employee.employeeID = $user.employeeID
      }
      

      然后你可以使用Export-Csv$users保存回一个文件。

      如果找不到用户帐户,您可能需要一些错误处理。

    【讨论】:

      【解决方案2】:

      使用 Calculated Property 通过再次查询广告来插入值:

      $CSVImport | 
          Select-Object @{
              Name = "employeeID"
              Expression = { 
                  try
                  {
                      (Get-ADUser -Filter "EmailAddress -eq '$($_.OwnerEmail)'" -Properties 'EmployeeID').EmployeeID
                  }
                  catch 
                  {
                      'Not Found'
                  }
              }
          }, *
      

      由于 AD cmdlet 是错误终止默认情况下,我将它放在 try{} catch{} 语句中,以便在通过电子邮件找不到用户时处理错误。

      【讨论】:

        猜你喜欢
        • 2022-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多