【问题标题】:Get-ADuser Script IssueGet-ADuser 脚本问题
【发布时间】:2019-02-01 18:20:37
【问题描述】:

我想将 CSV 文件中的电子邮件地址列表与我们的 Active Directory 进行比较。我很难将所有部分放在一起。

伪代码:

  • 将电子邮件地址列表导入到我公司活动目录的查询中。
  • 通过电子邮件地址加入。
  • 仅返回那些在 AD 中启用帐户的记录。

代码:

Import-Csv users.csv |
     Get-ADUser -Filter {mail -eq $_.mail } |
     where {$_.enabled -eq $true} |
     Select-Object -Property DistinguishedName, samaccountname, mail, enabled

我想我需要为 SearchBase 添加其他信息,但我不知道如何添加。

例如:-SearchBase "DC=na,DC=corp,DC=<company name>,DC=com"

CSV 文件中只有一列。列名是邮件。我尝试加入的 AD 属性是 mail。

【问题讨论】:

  • 这里有 4 个答案。他们中的任何一个解决了您的问题吗?

标签: powershell


【解决方案1】:

您可以将 csv 导入存储在一个变量中,并获取您拥有的 AD 中的所有电子邮件地址,在列表之间使用 Compare-Object,然后针对结果使用 foreach。

$matchingUsers = New-Object System.Collections.ArrayList
$csv = import-csv users.csv 
$emails = $(get-aduser -filter * -Properties mail,Enabled | ? {$_.Enabled -eq $True} | select mail).mail
$comparison = $($(compare-object -ReferenceObject $a -DifferenceObject $b -IncludeEqual)| ? {$_.SideIndicator -eq '=='}).InputObject
foreach($object in $comparison) {
     $user = Get-ADUser -Filter {mail -eq $object} -SearchBase "DC=na,DC=corp,DC=,DC=com" -Properties DistinguishedName,samaccountname,mail,enabled
     $matchingUsers.Add($user)
}

然后导出$matchingUsers,格式化成表格,之后随便你什么

【讨论】:

    【解决方案2】:

    将 csv 电子邮件作为字符串数组 ($array) 获取。选择 -expand 是您的朋友。

    从 AD 中获取用户,通过管道传输到 foreach-object,并询问 $array.Contains(管道输入对象的电子邮件地址属性)是否。

    如何处理该条件的结果取决于您;很多方法来处理它。但是你需要看到一种严肃的方式来得到你所要求的,这就是我提供的。

    【讨论】:

      【解决方案3】:

      我可能会这样做:

      # enter the DistinghuishedName of the OU where the users to check are in
      $SearchBase = 'DC=na,DC=corp,DC=,DC=com'
      $CsvPath    = 'PATH TO THE USERS.CSV FILE'
      
      $UserList   = Import-Csv -Path $CsvPath
      # for fast lookup, best use a Hashtable
      $MailLookup = @{}
      foreach ($user in $UserList) {
          $MailLookup[$user.mail] = $true   # the value is not important here
      }
      
      # in fact, the properties DistinguishedName,SamAccountName and Enabled are returned by default
      Get-ADUser -Filter * -SearchBase $SearchBase -Properties DistinguishedName,SamAccountName,EmailAddress,Enabled | 
           Where-Object {$_.Enabled -and $MailLookup.ContainsKey($_.EmailAddress) }
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        streamlineshorten trebleCode 的建议一点点你可以这样开始:

        $MailAddressList = Import-Csv -Path users.csv
        $AllUsersList = Get-ADUser -Filter * -SearchBase 'DC=na,DC=corp,DC=,DC=com' -Properties DistinguishedName,samaccountname,mail,enabled | Where-Object {$_.enabled}
        Compare-Object -ReferenceObject $MailAddressList -DifferenceObject $AllUsersList -Property 'Mail' -PassThru -IncludeEqual -ExcludeDifferent
        

        如果需要,您可以将-ReferenceObject-DifferenceObject 交换。我目前没有要测试的 Active Directory。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多