0

我有一个 txt 文件,其中包含格式为的名称列表

名字姓氏

如何仅通过从此列表中获取内容来检查用户是否存在于 AD 中。我在 AD 中最接近的属性是 displayName,其格式为姓氏、名字。下面不起作用。谢谢

get-content "c:\temp\file.txt" |
 foreach {
             if (Get-ADUser -Filter {displayName -like $_}){
                 Write-host "$_ does exist in AD"}
              else {
                   Write-host "$_ does not exist in AD"
                   }

 } 
4

3 回答 3

1

怎么样

    get-content "temp.txt" |
     foreach {
    $givenname,$surname = $_ -split ' '
                 if (Get-ADUser -Filter "surname -eq '$surname' -and givenname -eq '$givenname'"){
                     Write-host "$_ does exist in AD"}
                  else {
                       Write-host "$_ does not exist in AD"
                       }

     } 
于 2013-03-12T04:04:46.767 回答
0

这会更好吗?

get-content "c:\temp\file.txt" |
 foreach {
         $filter = [scriptblock]::create("DisplayName -like '*$_*'")
         if (Get-ADUser -Filter $filter){
             Write-host "$_ does exist in AD"}
          else {
               Write-host "$_ does not exist in AD"
               }

 } 
于 2013-03-12T01:19:14.787 回答
0

我会这样做:

$users = Get-Content "C:\temp\file.txt"

Get-ADUsers -Filter * -Properties givenName,sn | % {
  if ( $users -contains ($_.givenName + " " + $_.sn) ) {
    $_.Name + " exists in AD."
  } else {
    $_.Name + " does not exist in AD."
  }
}
于 2013-03-12T11:00:19.493 回答