0

I'm new to PowerShell. I'm trying to pull a users Active Directory info based on their job title and place it in a csv file. I have two corresponding arrays, $title and $csvfile. $title[0] corresponds with $csvfile[0] and $title[1] to $csvfile[1] and so on. This loop creates all my .csv files but doesn't pull any user data. I can manually enter any of the $title items into the script and it works but when used as an array I get nothing.

Does an array add any leading or ending spaces, quotations, delimiters, anything that might keep the -and statement from working?

$title = @(
   'Director of Nursing'
   'Assistant Director of Nursing'
   'Activities Director'
)
   $csvfile = @(
  'DON'
  'ADON'
  'ACTIVITIES'
)

    for($i=0; $i -lt $title.Count; $i++)
{
    #Get-ADUser -Filter { (Title -like "Director of Nursing")  -and  (Company -like "location1") }| Export-Csv c:\temp\$($csvfile[$i]).csv"
    Get-ADUser -filter { (Title -like "$($title[$i])")  -and  (Company -like "Location1") }| Export-Csv "c:\tempPath\$($csvfile[$i]).csv"
}
0

1 Answer 1

1

The AD module re-parses the -Filter block (and turns it into a proper LDAP filter), the likeliest explanation is that it can't resolve $($title[$i]) when that happens.

Try with a string filter instead:

Get-ADUser -filter "Title -like '$($title[$i])'  -and  Company -like 'Location1'"| ...
Sign up to request clarification or add additional context in comments.

3 Comments

@Mathias, where did you find this out from?
@AbrahamZinala I used ILSpy to decompile the AD module assembly and had a look at what it does
hmm, does it just break down the code they use? Sorry, cant get to it on our network at work. Might import it and have it read as a function so I can see the code if that's the case. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.