variable member in quotes

Welcome Forums General PowerShell Q&A variable member in quotes

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #81755

    Participant
    Points: 18
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

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

  • #81764

    Participant
    Points: 2
    Rank: Member

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

    Participant
    Points: 0
    Rank: Member

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

  • #81800

    Participant
    Points: 18
    Rank: Member

    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

      Participant
      Points: 1
      Rank: Member

      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

    Participant
    Points: 225
    Helping Hand
    Rank: 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

    Participant
    Points: 18
    Rank: Member

    can you elaborate on "have to do termination"?

The topic ‘variable member in quotes’ is closed to new replies.