I have ran the following code to query AD for either the first or second name. If I enter "conor" as the input I get the expected results which contains two users, but if I enter "timms" (surname of same user) I get the following error: Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a method named 'op_Subtraction'.
$exists = $true
while ($exists -eq $true) {
$search = Read-Host -Prompt "Enter first name or surname name of user"
$results = Get-ADUser -Filter "Name -like '*$search*'"
if ($results) {
Write-Output "You're in one"
for ($i = 0; $i -le $results.count - 1; $i++) {
$i
$results[$i] | Select-Object Name, SamAccountName | Format-Table
}
}else {
Write-Host "Name does not match any of the users in domain"
$exists = $false
}
}
Output with conor as input:
Enter first name or surname name of user: conor
You're in one
0
Name SamAccountName
---- --------------
Conor Timms Conor.Timms
1
Name SamAccountName
---- --------------
Conor Admin ConorAdmin
Output with timms as input:
Enter first name or surname name of user: timms
You're in one
InvalidOperation:
Line |
7 | for ($i = 0; $i -le $results.count - 1; $i++) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~
| Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a method named 'op_Subtraction'.
Enter first name or surname name of user: