1

Ok, i got another issue, probably having to do with not being able to store values properly. The original idea was to get a list of users and get their names separated into two values - givenName and sn so that Get-ADUser cmdlet would be able to use them through it's -Filter option.

While a great idea and solved, i have may have overlooked the fact that when people have 2 names or surnames, that isnt really an option as you cant predict that into a code easily. Another option i found is the attribute displayName. As our displayNames are equal to full name of the user, it would be the easiest option. However, i cant seem to successfully store the values from txt file into a variable for it to work.

My code currently looks like this:

Import-Csv -Path C:\temp\users.txt -header "displayName" |

ForEach-Object{ Get-ADUser -Filter "displayName -like '$($_.displayName)'" -Properties givenName, Surname, SamAccountName 
} |
Select-Object givenName, Surname, SamAccountName | 
Export-CSV C:\Temp\usersexport.csv -NoTypeInformation -Encoding UTF8
  1. The txt file has a structure like so:
John Smith Jones
Paula Ridley
Veronica Anne Downing
etc...

So we arent really talking about a CSV file and the names of users are each in a separate line.

  1. The import works fine, the issue in my opinion is with the introduction of pickedup value from the file and it not being either an actual string or somehow not being registered by the -Filter option.

  2. In the code above i have removed a variable that was used to store the values as there was no change even if it was there.

  3. I have also tried specifing the -Delimiter "`n" in order to specify the separation of values inside the txt file on new each line, but either i didnt use the write symbol or that doesnt work. EDIT:

The issue that happens after i run my code is my exported CSV file only has the following entry: . I have no idea why to be honest and it doesnt change no matter what, but like i said it could entirely be possible that the format in which the names inside the txt file are is faulty.

If i understand correctly, -Filter runs the given value into a string anyway, so i suppose the issue of txt file names not being specially formated before hands isnt whats causing this.

Jervis
  • 39
  • 5

2 Answers2

0

If each line in the file is just a name, you would be better off just doing Get-Content rather than Import-Csv for the file.

The problem with it not being picked up by the Foreach-Object loop is that you're not connecting the loop with the first line of your script, so the $_ pipeline variable is not being populated.

If you wish to go with the Import-Csv you need to refactor it with a pipe, like this:

Import-Csv -Path 'C:\temp\users.txt' -header "displayName" | ForEach-Object{ Get-ADUser -Filter 'displayName -like "$($_.displayName)"' -Properties givenName, Surname, SamAccountName }

As I said you could also use Get-Content, like this:

Get-Content -Path 'C:\temp\users.txt' | ForEach-Object{ Get-ADUser -Filter 'displayName -like "$_"' -Properties givenName, Surname, SamAccountName }

Or:

$DNames = Get-Content -Path 'C:\temp\users.txt' 
ForEach ($DName in $Dnames){ 
Get-ADUser -Filter 'displayName -like "$Dname"' -Properties givenName, Surname, SamAccountName 
}

Be mindful that variable expansion only happens within double quotes, not single quotes.

Laage
  • 135
  • 9
  • the missing | was a typo, my error. However, none of your code seems to work. Also, im not entirely sure that i phrased it correctly so fixed the explanation a bit to make it more clear. – Jervis Aug 01 '20 at 19:38
  • Are the names in the file exact matches for valid AD users? If so you might want to use -eq rather than -like. If not you need to use wildcards in your Get-ADUser filter. – Laage Aug 01 '20 at 20:04
  • 2
    Your single-quoted filters won't work - see https://stackoverflow.com/a/44184818/45375 – mklement0 Aug 01 '20 at 20:20
0

EDIT: Believe it or not, the most insanely stupid thing of all - the fact that displayName did not have a " " between separate values in my txt file but a TAB space instead, due to the fact i imported my displayNames from an Excel table and joined them together manually. I am seriously considering to hire a shrink because i spent 5 hours and didnt notice this.

Thank you both @Laage and @mklement0 for your effort!

Jervis
  • 39
  • 5