9

I'm trying to extract select sets of users with Get-ADUser. The users belong to companies whose names include non-ASCII characters, e.g. "Gåäördet". Unfortunately, we do have to use the company property for this task and we also need it to work from a script.

The following works great in an interactive session but returns no data when executed within a script:

$Company = "Gåäördet"
Get-ADUser -Filter "company -eq '$Company'"

The workarounds I've found work but are not reliable enough (risks selecting wrong objects):

# Work-around 1:
$Company = "Gaaordet" # Replace åäö with aao in the variable
Get-ADUser -Filter "company -eq '$Company'" # Matches the company "Gåäördet", but why?

...or...

# Work-around 2: 
$Company = "G...rdets" # Use regex for åäö
Get-ADUser -Filter * -Properties Company | ? Company -match "$Company"

For additional note: character encoding might not be the issue here. As suggested in a comment I put this within a script. Read the comment for each Get-ADUser-line:

$OutputEncoding = [Console]::OutputEncoding

$Company = "aao"
Get-ADUser -Filter "company -eq '$Company'" # Matches company "åäö"

$Company = "åäö"
Get-ADUser -Filter "company -eq '$Company'" # No matches

I was hoping some of you could offer a better solution to this conundrum.

5
  • Thank you @RyanRies, unfortunately it didn't seem to help. Read my edit for more info. May 22, 2015 at 13:51
  • 1
    So I can (unfortunately) reproduce this behavior with LDP.exe, so our problem may be at a deeper level than Powershell.
    – Ryan Ries
    May 22, 2015 at 21:55
  • Instead of -Filter would using -LdapFilter work? Example: -LdapFilter "(company=$Company)" May 24, 2015 at 0:42
  • @TessellatingHeckler, still no matches on "Gåäördet". May 25, 2015 at 9:19
  • @user2320464, LdapFilter produces no matches either. May 25, 2015 at 9:19

3 Answers 3

6

Thanks to all the help here I got to the bottom of this odd behavior, much appreciated!

Turns out the "-Filter" argument accepts "åäö" interchangeably with "aao". This is not the doing of PowerShell but further down the stack (thanks @RyanRies for looking into it). That's the reason why the following snippet works:

$Company = "aao"
Get-ADUser -Filter "company -eq '$Company'" # Matches company "åäö"

It also turns out the query is not case sensitive, so this works too:

$Company = "AaO"
Get-ADUser -Filter "company -eq '$Company'" # Matches company "åäö"

Actually, "åäö" works too as long as it is a unicode query (thanks @Daniel):

$Company = "$([char]0x00E4)$([char]0x00E5)$([char]0x00F6)" # "åäö"
Get-ADUser -Filter "company -eq '$Company'" # Matches company "åäö"

In the end this leaves us with two options:

  • Replace "åäö" with "aao" in your queries. The output will be identical to using "åäö".
  • Replace "åäö" with unicode (@joel-coel, thanks for the nudge), e.g. with a script.

I chose to go with the second option and the outcome looks a bit like this:

function UniReplace($n){
    [char][int]"0x$n"
}

$Company = "åäö"
$Company = $Company -Replace 'ä',"$(UniReplace E4)"
$Company = $Company -Replace 'Ä',"$(UniReplace C4)"
$Company = $Company -Replace 'å',"$(UniReplace E5)"
$Company = $Company -Replace 'Å',"$(UniReplace C5)"
$Company = $Company -Replace 'ö',"$(UniReplace F6)"
$Company = $Company -Replace 'Ö',"$(UniReplace D6)"

echo "This is the content of string `$Company: $Company"
Get-ADUser -Filter "company -eq '$Company'"

I guess that is as good as it gets for now.

2

I might help you with a workaround.

Create a Unicode encoded text file and insert the Company name. Then use Get-Content to store the company name in a variable.

$companyName = Get-Content .\companyName-unicode.txt
Get-ADUser -Filter { company -eq $companyName }

I tested with chinese text (中國哲學書電子化計劃) it and it worked on my server.

3
  • Thanks @Daniel, that workaround did indeed work. However, it is seriously dirty. Let's see it if anyone else has some input too. May 22, 2015 at 16:56
  • Yeah, I know. I hope someone got a proper answer to your prayer - er - problem. :)
    – Daniel
    May 22, 2015 at 22:43
  • @TessellatingHeckler, when doing your tests, what non-ASCII characters are you trying to match? May 25, 2015 at 9:21
1

You might try building the names via code point surrogates:

https://stackoverflow.com/questions/4834291/how-to-encode-32-bit-unicode-characters-in-a-powershell-string-literal

It's not much better, but at least it allows you to contain the entire script within the source file.

1
  • this was of big help. I'm doing a write-up on the issue right now. May 25, 2015 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .