Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

multiple AD domain recursing search from datatable

$
0
0

Hi all,

Can someone help about this problem :

I have a datatable in a datagridview, I want to search the username from the mail address.

I'm able to find what I need if my datatable have only users in a single domain but if I have 2 mail addresses with 2 different domains I have only records from the last line.

I do not know how to recurse the search for each line and pass the searchroot property according to the value of User_LDAP_Mail column

I hope I'm clear...

here is my function :

Function LDAPSearchUserNameViaMailAddress
{

$DataGrid.Name = $MyInvocation.MyCommand
AddRemoveColumns -TableType $DataGrid.Name

# Build LDAP Query with the mail address #

[String]$ldapQuery = "(&(objectCategory=user)(|"

$data = $global:dt.Rows | Where {(($_.User_LDAP_Mail -ne "") -and ($_.User_LDAP_Mail -ne $null) -and ($_.User_LDAP_Mail -ne [System.DBNull]::Value))}
$data | % {
		$ldapQuery = $ldapQuery + "(mail=" + $_.User_LDAP_Mail + ")"
		if ($_.User_LDAP_Mail -match "@domain1.com") {$SearchRoot = "LDAP://dc=domain1,dc=forest,dc=com"}
		else { $SearchRoot = "LDAP://dc=domain2,dc=forest,dc=com"}
}
$ldapQuery = $ldapQuery + "))"
if ($data -ne $null)
{
	$data.item |Get-Member -Verbose |Out-GridView -Title "data"
	$de = new-object system.directoryservices.directoryentry($SearchRoot)
	$ads = new-object system.directoryservices.directorysearcher -argumentlist $de,$ldapQuery
	$ads.PageSize=1000
	$complist = $ads.findall()
	$complist | % {
		#$_.properties.name; $_.Path 
		$adsresult = $_
		$global:dt.Rows | Where {$_.User_LDAP_Mail -eq $adsresult.properties.mail} | % {
			$dtrow = $_
			$dtrow.UserName = $adsresult.properties.samaccountname[0]
			$dtrow.User_LDAP_Name = $adsresult.properties.name[0]
			$dtrow.User_LDAP_Mail = $adsresult.properties.mail[0]
			#$dtrow.User_LDAP_DN = $adsresult.Path 
			[String]$str_LDAP_DN = $adsresult.Path
			$dtrow.User_OU = $str_LDAP_DN.Substring($str_LDAP_DN.IndexOf("OU="))
		}
	}
}
}


Thanks



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>