resolve DN using anr

I have tested and this works fine:


Get-ADUser -LDAPFilter “(anr=Lastname Firstname)” | select DistinguishedName

…but wish to use it in a ForEach loop to read a .csv file

$mgrs = Import-Csv .\Managers.csv

  foreach ($mgr in $mgrs) {
Get-ADUser -LDAPFilter "(anr=$mgr)" | select DistinguishedName
}

I’m not sure how to pass that variable into the -Filter and pull out all the associated DN’s

foreach ($mgr in $mgrs) {
Get-ADUser -LDAPFilter "(anr=$($mgr))" | select DistinguishedName
}

See also this discussion

https://social.technet.microsoft.com/Forums/windowsserver/en-US/04a45926-5f8a-467d-a867-7f8a7a13be4c/expanding-variable-inside-filter

postanote,

No error returbed but no data either.

My .csv is in this format

Managers

Lastname Geoffrey
Lastname Geoffrey
Lastname2 Sandy
Lastname2 Sandy
Lastname2 Sandy
Lastname2 Sandy
Lastname2 Sandy

So your CSV is missing a header entry? CSVs need a column name for each column. They also get imported as objects, not strings. If column names are missing, I believe the cmdlet will give you a warning and auto-name them.

So… let’s say your CSV header is “LastName,FirstName” or something like that. Those become the properties of the objects – each row is one object in the resulting array.

With that in mind…

$ManagerList = Import-Csv '.\Managers.csv'

foreach ($Manager in $ManagerList) {
    Get-ADUser -LDAPFilter "(anr=$($Manager.FirstName) $($Manager.LastName))" | 
        Select-Object -Expand DistinguishedName
}

OK thanks I got it to work with your initial script and the logic I was after.

~I only wanted a single column header named ‘managers’
~under the header, I added full names (including spaces), with no quotes and no comma after the row
Managers
Smith Joe
Jones Sally
Gates Bill

I imported the .csv into a variable, slighted modified your -LDAPFilter:

$ManagerList = Import-Csv .\Managers_ANR.csv

foreach ($Manager in $ManagerList) {
    Get-ADUser -LDAPFilter "(anr=$($Manager.Managers))" | 
        Select-Object -Expand DistinguishedName
}

I was then able to output the corresponding DN of each of the managers resolved via ANR.

Thank you.