2
votes

In part of my script I am trying to query AD Searching by employee number. This works fine for 90% of the employees. however in my organization we have a few special employees where the employee number contains letters thus breaking my powershell command.

here are 2 examples

   Get-ADUser -Filter "EmployeeID -eq 12345" -Properties SAMAccountName 
    Get-ADUser -Filter "EmployeeID -eq ABC1234567" -Properties SAMAccountName 

The first option works fine but the second one fails.

Get-ADUser : Error parsing query: 'EmployeeID -eq ABC1234567' Error Message: 'syntax error' at position: '16'. At line:1 char:1 + Get-ADUser -Filter "EmployeeID -eq ABC1234567" -Properties SAMAccou ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr osoft.ActiveDirectory.Management.Commands.GetADUser

1
Let me know if that doesn't work! :)Dejulia489

1 Answers

2
votes

12345 is treated as an integer, so the filter query doesn't fail.

Get-ADUser -Filter "EmployeeID -eq 12345" -Properties SAMAccountName 

Adding ABC1234567 to the filter makes the filter a string, the string needs to be wrapped in quotes. Try the command below...

 Get-ADUser -Filter "EmployeeID -eq 'ABC1234567'" -Properties SAMAccountName