variable member in quotes

This topic contains 7 replies, has 5 voices, and was last updated by  John Curtiss 6 days, 7 hours ago.

  • Author
    Posts
  • #81755

    John Curtiss
    Participant

    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?

  • #81757

    Naw Awn
    Participant

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

  • #81764

    Will Prather
    Participant

    "." 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.

  • #81775

    Richard Siddaway
    Moderator

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

  • #81800

    John Curtiss
    Participant

    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.

    • #81848

      Naw Awn
      Participant

      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)")"

  • #81872

    postanote
    Participant

    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.

  • #81950

    John Curtiss
    Participant

    can you elaborate on "have to do termination"?

You must be logged in to reply to this topic.