locked
Using variable within -Filter to find groups' "info" containing variable RRS feed

  • Question

  • Hello,

    I am having all sorts of issues. My goal is to filter ad groups and return all groups that have certain information in the info attribute.

    This is an example of what I am doing

    #Find AD groups with a user's ID or Name in notes
    
    $User = Read-Host "Enter the users ID"
    $Name = (Get-ADUser -Identity $User -Properties *).displayName
    
    Get-ADGroup -Filter { ("info" -contains $User) -or ("info" -contains $Name) } -Properties * | select sAMAccountName, info, managedBy | Export-Csv c:\Temp\TEST.csv -NoTypeInformation

    I keep getting the error below:

    Get-ADGroup : Error parsing query: ' ("info" -contains $User) -or ("info" -contains $Name) ' Error Message: 'syntax error' at position: 
    '3'.

    I have tried numerous suggestions that I have found on-line such as putting " " around the variable... dropping the brackets and using " and '
    No luck!

    Tuesday, August 11, 2015 3:40 PM

Answers

  • Hi,

    First off, drop the -Properties * from your Get-ADUser command and only return the property you need.

    This should do what you're after:

    $user = Read-Host 'Enter user'
    $userName = (Get-ADUser $user -Properties DisplayName).DisplayName
    
    Get-ADGroup -Filter "info -like '*$user*' -or info -like '*$userName*'"


    Tuesday, August 11, 2015 3:59 PM

All replies

  • Hi,

    First off, drop the -Properties * from your Get-ADUser command and only return the property you need.

    This should do what you're after:

    $user = Read-Host 'Enter user'
    $userName = (Get-ADUser $user -Properties DisplayName).DisplayName
    
    Get-ADGroup -Filter "info -like '*$user*' -or info -like '*$userName*'"


    Tuesday, August 11, 2015 3:59 PM
  • That did it! You rock fine sir. quick question (because I like to understand the 'why'), what is the purpose of putting the splat before and after variable inside single quotes like that?
    Tuesday, August 11, 2015 5:28 PM
  • what is the purpose of putting the splat before and after variable inside single quotes like that?

    PS C:\> help about_Quoting_Rules
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 11, 2015 5:36 PM
  • Cheers.

    As for your question, I think you've got your terms mixed up a bit. I'm not using a splat anywhere, I'm just using a string for the filter (which is generally the preferred method) instead of a scriptblock. If you're referring to the wildcard (*), I'm using that because -like requires it.


    Tuesday, August 11, 2015 5:39 PM