2013-03-12 78 views
0

我有一個包含名字的格式列表的txt文件確定是否AD帳戶存在基於名姓

名姓

我如何可以檢查用戶是否在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" 
        } 

} 

回答

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" 
         } 

    } 
+0

這個工作,謝謝 – user1938745 2013-03-12 18:07:27

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" 
       } 

} 
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." 
    } 
} 
相關問題