0

I'm quite desperate for tips here. Here's my quandary:

First, I had an array of string, which I created this way (showing a subset of the numbers):

$a = @"
00013120747
00013051436
00013110491
00002100011
"@

$aa = $a.Split("`n")

Next, I generate a list of all users in Active Directory (using ActiveRoles) this way:

$all_u = Get-QADUser -DontUseDefaultIncludedProperties -IncludedProperties Name,LogonName,EmployeeID -SizeLimit 0

Now, why can't I match against an element of the $aa array? For example, doing the following:

$all_u | where {$_.EmployeeID -match "00013110491"}

it works. But if I do the following:

$all_u | where {$_.EmployeeID -match $aa[2]}

it doesn't work.

So I did a simpler test:

$aa.GetType().Name
String[]

$aa[2].GetType().Name
String

$aa[2]
00013110491

$aa[2] -eq "00013110491"
False

What?? What's going on here???

I'm using PowerShell ISE, by the way.

3 Answers 3

2

If you examine the elements of $aa carefully, you'll find they all have trailing whitespace. This is a consequence of doing the split on "`n". If you trim them after you do the split, you'll get the expected result.

$a = @"
00013120747
00013051436
00013110491
00002100011
"@

$aa = $a.Split("`n") |% {$_.trim()}

$aa[2] -eq "00013110491"

True

The -match will match anywhere in the string, so it will still match even with the trailing space. The -eq requires and exact match, character for character, and the trailing space will cause it to return False.

Sign up to request clarification or add additional context in comments.

1 Comment

Whoa, nice find! I actually tried foreach ($elem in $aa) { "=={0}==" -f $elem } and each line doesn't show any trailing space. Thanks for telling me! Marking yours as the answer.
1
$a = @"
00013120747
00013051436
00013110491
00002100011
"@

$aa = $a.Split("`r`n")

Comments

-2

You're using a here string. That syntax like this:

$String = @"
entry1
entry2
entry3
"@

$String.Count
 >1

You need to Split your here string first before you can compare them. If you look at what you actually get when you run

 $a[2]
 > '0'

You're asking PowerShell for the 2nd index position. That means this. 00[0]13120747

This is because a here-string is a single string, which that happens to contain multiple lines. With PowerShell array indexing, if there is only one single entry, you'll get back that position from the list.

$string = "0123456"
$string[0..2]
 >012

2 Comments

Didn't you see that I have done exactly that? $aa = $a.Split("`n")
I missed that. It looks like Mjolinor was correct here, this seems to be an error in the ISE, injecting a space in the end of every character. Checking the length of each entry, they are twelve chars long in the ISE, vs 11 when working in the Console.

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.