2015-12-21 56 views
0

我試圖從活動目錄中獲取所有用戶的全名,這些活動目錄基於特定的登錄腳本ex:「Staff」,而不是基於組織單位的活動目錄中的配置文件中。如何使用PHP在LDAP目錄中搜索某些登錄腳本?

這裏是我的代碼:

<?php 
// active directory 
$ldap_host = "xxx"; 
$ldap_port = "xxx"; 
// Active Directory DN 
$ldap_dn[] = "ou=Staff,DC=xxx,DC=xxx,DC=xx"; 
$ldap_dn[] = "ou=Faculty,DC=xxx,DC=xxx,DC=xx"; 
// Domain, for purposes of constructing $user 
$ldap_usr_dom = "@xxx.xxx.xx"; 
// connect to active directory 
$ldap   = ldap_connect($ldap_host, $ldap_port); 
$ldap_id[] = $ldap; 
$ldap_id[] = $ldap; 
$username  = "xxx"; 
$password  = "xxx"; 
// verify user and password 
if ($bind = @ldap_bind($ldap, $username . $ldap_usr_dom, $password)) { 
    $filter = "(objectCategory=person)"; 
    $result = ldap_search($ldap_id, $ldap_dn, $filter) or exit("Unable to search LDAP server"); 
    foreach ($result as $value) { 
     if (ldap_count_entries($ldap, $value) > 0) { 
      $search = $value; 
      break; 
     } 
    } 

    if ($search) { 
     $entries = ldap_get_entries($ldap, $search); 
     for ($x = 0; $x < $entries['count']; $x++) { 
      if (!empty($entries[$x]['cn'][0])) { 
       $ad_users[$x] = $entries[$x]['cn'][0]; 
       print_r($ad_users); 
       echo $ad_users[$x]."<br>"; 
      } 
     } 
    } 
    ldap_unbind($ldap); // Clean up after ourselves. 
} 

$m .= "Retrieved " . count($ad_users) . " Active Directory users\n"; 
echo $m; 
?> 

我的代碼檢索下單位的工作人員所有用戶都使用不同的登錄腳本有些是工作人員和其他有網友。

+0

那麼,什麼是真正的問題嗎? – Epodax

+0

@Epodax如何獲取只有在活動目錄中的配置文件中具有Staff登錄腳本的用戶? – Learner

+0

腳本如何分配給用戶?通過組策略還是手動添加到AD中的配置文件? – RamRaider

回答

0

你可以嘗試,爲LDAP過濾器時,也許以下幾點:

$script='staff.vbs'; 
$filter = "(&(objectCategory=person)(objectClass=user)(scriptPath={$script}))"; 
+0

非常感謝你^ _^ – Learner

+0

主席先生最後一個問題:如果登錄腳本不止一個單詞,例如說:Staff和Staff_Logos。如何編輯你的線條? – Learner

+0

您應該可以在表達式中使用通配符。 http://stackoverflow.com/questions/9564120/using-wildcards-in-ldap-search-filters-queries或http://forums.asp.net/t/1029112.aspx?How+to+query+LDAP+使用+ LIKE +聲明 – RamRaider