Mäx Müller
Mäx Müller

Reputation: 253

Get-ADUser returns nothing

I'm currently working on a script which extracts some names stored in a .csv file and should display some information about the AD account of that user.

The names may be stored like this han solo or solo han, but always divided by a space. Those names can contain at least umlauts and who the hell knows what else.

Every user has an attribute called name, which looks like this solo, han (essentially "surname, given name").

This is the line I'm currently using:

Get-Content -Encoding:string C:\Temp\admin.mh\Reports\User_AD.csv |
select -skip 1 | 
ConvertFrom-Csv -Header "User" -Delimiter "," | %{
   $arr = $_.User.Split(" "); 
   Get-ADUser -Filter {(Name -eq "$($arr[1]), $($arr[0])") -or (Name -eq "$($arr[0]), $($arr[1])")} 
}

The first bit is somewhere from Google, which stated that this is the way to get the incoming encoding right.

If I manually type in the name (even with umlauts) I get the right result, but when I store it in the array I never get any results, even with no special characters at all.

I was able to reproduce this exact behaviour on the commandline by creating the array like this $arr = @("han", "solo") and using the exact same filter. If I leave the variables out and filter manually like name -eq "solo, han" everything works fine.

Upvotes: 0

Views: 2506

Answers (1)

Vesper
Vesper

Reputation: 18757

Get-ADUser does not support script block filtering, instead you should use a string-based filtering. But, you can use -eq and other comparison operators in the filter. Also Get-ADUser requires string values to be surrounded by single quotes. So your filter should be like this:

Get-ADUser -Filter "(Name -eq '$($arr[1]), $($arr[0])') -or (Name -eq '$($arr[0]), $($arr[1])')" 

Upvotes: 3

Related Questions