Get-ADUser -SearchBase from CSV file

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Dan Potter Dan Potter 1 year, 10 months ago.

  • Author
    Posts
  • #21999
    Profile photo of Lynn Alvey
    Lynn Alvey
    Participant

    I am having a problem importing OUs from a CSV file to use with Get-ADUser. The script works fine when I create an array in the script but won't work when I import from the csv file.

    Script with Array:

    ##Set location to groups subfolder
    Push-Location  'D:\PIV Script\OU List'
    
    ##Import AD Module if needed
    Import-Module ActiveDirectory -ErrorAction SilentlyContinue
    
    $file = @()
    
    $OUs = 'OU=AWA,OU=AGC,OU=LOB,DC=test,DC=gov',
        'OU=AWA,OU=ASH,OU=LOB,DC=test,DC=gov', 
        'OU=AWA,OU=ATO,OU=Win7 LOB,DC=test,DC=gov'
    
        $OUs | ForEach-Object {
        $Users = Get-ADUser -SearchBase "$_" `
            -Properties emailAddress, Manager `
            -LDAPFilter '(extensionAttribute11=Test)'
    
        $file = $file += $Users
        }
    $file | select-Object DistinguishedName, emailAddress, name, SamAccountName, UserPrincipalName, Manager | Export-Csv -NoType "Users AMC.csv"

    Script with CSV-Import:

    ##Set location to groups subfolder
    Push-Location  'D:\PIV Script\OU List'
    
    ##Import AD Module if needed
    Import-Module ActiveDirectory -ErrorAction SilentlyContinue
    
    $file = @()
    
    ##Import objects from CSV file 
    $OUs = @(Import-Csv "AMC OUs.csv")
    
        $OUs | ForEach-Object {
        $Users = Get-ADUser -SearchBase $_ `
            -Properties emailAddress, Manager `
            -LDAPFilter '(extensionAttribute11=FAA)'
    
        $file = $file += $Users
        }
    $file | select-Object DistinguishedName, emailAddress, name, SamAccountName, UserPrincipalName, Manager | Export-Csv -NoType "Users AMC.csv"

    Thank you,

  • #22000
    Profile photo of Lynn Alvey
    Lynn Alvey
    Participant

    The error I get when I run the script with the CSV file is:

    Get-ADUser : The supplied distinguishedName must belong to one of the following partition(s):...
  • #22002
    Profile photo of Sean Quinlan
    Sean Quinlan
    Participant

    What does your CSV look like?

    When you run Import-CSV then every line in the CSV will come back as an object with one or more properties. The headers in the CSV file are the names of the properties. You will need to reference the property that has the OU name in it, rather than the entire object, even if each line is only one field.

    For example, if your CSV looked like this:

    OU
    "OU=AWA,OU=AGC,OU=LOB,DC=test,DC=gov"
    "OU=AWA,OU=ASH,OU=LOB,DC=test,DC=gov"
    "OU=AWA,OU=ATO,OU=Win7 LOB,DC=test,DC=gov"

    Then you'd have to change

        $Users = Get-ADUser -SearchBase $_ `

    to

        $Users = Get-ADUser -SearchBase $_.OU `
  • #22004
    Profile photo of Dan Potter
    Dan Potter
    Participant

    The arrays are unnecessary.

    function exportous

    $OUs = Import-Csv "AMC OUs.csv"

    foreach($i in $ous){

    $Users = Get-ADUser -SearchBase $i.ou `
    -Properties emailAddress, Manager `
    -LDAPFilter '(extensionAttribute11=FAA)'

    }

    exportous | Export-Csv -NoType "Users AMC.csv"

  • #22010
    Profile photo of Lynn Alvey
    Lynn Alvey
    Participant

    Thank you for your assistance. It turns out the problem was in the csv file. Because the OU has ","s in the path, the file had quotes around each OU. Import-CSV was including the quotes in the search. By removing the quotes and using Get-Content instead of Import-CSV I was able to get it to work.

    Final Script:

    ##Set location to groups subfolder
    Push-Location  'D:\PIV Script\OU List'
    
    ##Import AD Module if needed
    Import-Module ActiveDirectory -ErrorAction SilentlyContinue
    
    ##Process Groups
    $Group = @(Get-Content "Groups.csv")
        
        $Group | ForEach {
            $file = @()
            ##Import objects from CSV file 
            $OUs = @(Get-Content "$_ OUs.csv")
    
                $OUs | ForEach {
                $Users = Get-ADUser -SearchBase $_ `
                    -Properties emailAddress, Manager `
                    -LDAPFilter '(extensionAttribute11=FAA)'
    
                $file = $file += $Users
                }
            $file | select-Object DistinguishedName, emailAddress, name, SamAccountName, UserPrincipalName, Manager | Export-Csv -NoType "$_ Users.csv"
        }
    
  • #22014
    Profile photo of Dan Potter
    Dan Potter
    Participant

    you changed from import-csv to get-content which would allow you to use $_ instead of a column header. .

    Don't use arrays where not needed. @() means array.

    Try this

    PS H:\> $ous = @( gc .\ous.csv)
    PS H:\> $ous.gettype()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True Object[] System.Array

    PS H:\> $ous = gc .\ous.csv
    PS H:\> $ous.gettype()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True Object[] System.Array

You must be logged in to reply to this topic.