Reputation: 6818
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.
Upvotes: 0
Views: 221
Reputation: 13567
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
Upvotes: -2
Reputation: 68341
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.
Upvotes: 2
Reputation: 46
$a = @"
00013120747
00013051436
00013110491
00002100011
"@
$aa = $a.Split("`r`n")
Upvotes: 1