Author Posts

January 20, 2015 at 7:06 am

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,

January 20, 2015 at 7:22 am

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):...

January 20, 2015 at 8:27 am

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 `

January 20, 2015 at 9:22 am

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"

January 20, 2015 at 10:50 am

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"
    }

January 20, 2015 at 12:37 pm

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