Author Posts

October 10, 2017 at 10:49 pm

two-column array, one column is 'member'. doing a foreach ($pair in $array).

trying to filter get-adobject.

get-adobject -filter 'name -like $pair.member'
get-adobject -filter 'name -like "$pair.member"'

don't work.

but if I do
$thing = $pair.member
get-adobject -filter 'name -like $thing'

it works.

I vaguely remember there's some trick to using variable members inside of quotes. very vaguely. maybe there were parentheses involved. how do I get $pair.member directly into that filter without putting it into another variable?

October 10, 2017 at 10:59 pm

Try this?
get-adobject -filter {name -like "$($pair.member)"}

October 11, 2017 at 1:01 am

"." Is not a valid character for a variable, the parser resumes the string when it see ".member".

What Naw suggested is correct. "$($object.property) rest of string" will force PowerShell to evaluate the expression in parentheses first and then place the value into the string.

October 11, 2017 at 9:34 am

Its a quirk of the AD cmdlets that filter is awkward to use

October 11, 2017 at 3:16 pm

Richard is correct. Naw's suggestion is actually how I vaguely remembered the $variables in quotes" trick to work, but it does not return anything.

let's assume $pair.member is "windows admins" without the quotes.

get-adobject -filter {name -like "windows admins"}

returns my ad group called "windows admins"

get-adobject -filter {name -like "$($pair.member)"}

returns nothing.

October 11, 2017 at 10:31 pm

This is one thing I hate to use when the filter doesn't work
get-adobject -filter * | where {$_.name -like "$pair.member"}

Or do what you originally suggests using a variable
$thing = $pair.member

have you also tried?
get-adobject -LDAPFilter "(name = "$($pair.member)")"

October 12, 2017 at 4:02 am

And not just the Ad cmdlets. I've have run into this is others as well.

It must be remembered, at certain points in your code in your scripts, quoting a dotted variable, merely turns it in to a plain string.

This is one of the reasons I live in the PowerShell_ISE vs the PowerSHell console, because of the color coding, it is real obvious when stull like this will fail.

If you take any of the code segments shared thus far and paste them in the ISE, you will immediately see the difference.
This {$_.name -like "$pair.member"} is a string as noted by the darkred color of the dotted property.
This "(name = "$($pair.member)")" is a concatenated string using a separate dotted variable, as noted by the black color of the dotted variable.

We all know the since quotes are string stuff, and is you really do not need variable expansion, don't use double quotes.
If you need variable expansion, then of course that is what double quotes are for, with the exception of many filter use cases, like this one, where you have to do termination.

October 12, 2017 at 9:49 pm

can you elaborate on "have to do termination"?