resolve DN using anr

Welcome Forums General PowerShell Q&A resolve DN using anr

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #103813

    Participant
    Points: 0
    Rank: Member

    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

  • #103816

    Participant
    Points: 15
    Rank: Member
    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

    • #103996

      Participant
      Points: 0
      Rank: Member

      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

  • #103999

    Participant
    Points: 2
    Rank: Member

    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
    }
    • #104003

      Participant
      Points: 0
      Rank: Member

      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.

The topic ‘resolve DN using anr’ is closed to new replies.