1
votes

Trying to find a user's email from the properties of object made with Get-AdUser

Get-ADUser -Filter {EmailAddress -eq $userobject.GivenName + '.' + $userobject.SurName + '@overflow.com'}

Get-ADUser : Error parsing query: 'EmailAddress -eq $userobject.GivenName + '.' + $userobject.SurName + '@nfiindustries.com'' Error Message: 'Operator Not supported: ' at position: '40'. At line:1 char:1 + Get-ADUser -Filter {EmailAddress -eq $userobject.GivenName + '.' + $u ...

2
build the thing on the left of the -eq BEFORE you use it - and then use the $Var it is in instead of the complex objects you are using now. ///// also, the Get-ADUser command uses a STRING [not a scriptblock] for the -Filter parameter. you will have better, most consistent results if you use a string instead of the often-shown [but very] wrong scriptblock.Lee_Dailey

2 Answers

2
votes

The -Filter value behaves more like a a string, and even though it tries very hard to look like a script block with all the curly braces and Powershell-style operators, it's not a script block. Therefore most of the syntax that would work in normal PowerShell script code is invalid for a filter.

It's best when you treat it as a string. Pre-calculate any values you want to filter for, this makes everything much easier to handle.

$email =  $userobject.GivenName + '.' + $userobject.SurName + '@overflow.com'

Get-ADuser -Filter "EmailAddress -eq $email"
2
votes

To make an inline filter as you are attempting, you need to use the subexpression operator $(). This will allow PowerShell to process everything inside before handing it off to the -Filter parameter.

Get-ADUser -Filter "EmailAddress -eq '$($userobject.GivenName + '.' + $userobject.SurName + '@overflow.com')'"

Ultimately, once PowerShell performs its variable expansion and string interpolation, the filter is expecting to be Property -operator 'Value' or Property -operator "Value". How you get to that condition can vary.

As Tomalak's answer mentioned, the -Filter parameter accepts a string value rather than a script block. Why the command creators list that ({}) as the preferred syntax in the documentation is a mystery. Since it accepts a string and you rarely want the contents inside the string to be literal, it is best to surround the filter in double quotes. Then use single quotes inside. Sometimes complex filters will have a mix of single and double quotes, but you just have to watch for one opening quote finding an unintentional closing quote. The reason the multiple sets of quotes are even needed is because the -Filter contains comparison operators that expect the right-hand sides of those operators to be quoted. If you don't mix your quote types, you will have to come up with more creative ways to bypass interpolation. See below for some examples of this behavior. Note that the Double Quotes Outside while Escaping... and Double Quotes Outside and Single Quotes Inside scenarios are the ones that make -Filter receive what it wants. Single quotes outside of a string will not allow variables to expand ($str will just be literally $str).

Double Quotes:

PS> "string with double quotes"
string with double quotes

Single Quotes:

PS> 'single quotes'
single quotes

Double Quotes Outside and Single Quotes Inside:

PS> "outside doubles 'inside singles'"
outside doubles 'inside singles'

Double Quotes Outside and Inside:

PS> "double "double quotes""
At line:1 char:10
+ "double "double quotes""
+          ~~~~~~
Unexpected token 'double' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
With all that said, it is just more readable to do your string building outside of the filter first.

Single Quotes Outside and Double Quotes Inside:

PS> 'outside singles "inside doubles"'
outside singles "inside doubles"

Double Quotes Outside with Single Quoted Variable Inside:

PS> $str = "variable string"
PS> "outside doubles single variable '$str'"
outside doubles single variable 'variable string'

Single Quotes Outside with Double Quoted Variable Inside:

PS> 'outside singles double variable "$str"'
outside singles double variable "$str"

Double Quotes Outside while Escaping Double Quotes Inside:

PS> "fancy escaping with variable ""$str"""
fancy escaping with variable "variable string"

PS> "fancy escaping with variable `"$str`""
fancy escaping with variable "variable string"