locked
Variable does not work with -Filter attribute (See example below) RRS feed

  • Question

  • Hello Everyone,

    $name = "someone"
    Get-Mailbox -Filter {Extensioncustomattribute4 -eq $name} 

    Does not work. It seems like "-Filter" does not like to take variables.

    Is there a solution for this? if this doesn't work It means I can't use it on a script!

    Thank you all in advance ;-)


    I have a blog! http://unrevealingbox.blogspot.com Where Tips and Tricks comes together to form real walk-through guides.


    • Edited by Heyvoon Wednesday, June 7, 2017 2:16 PM
    Wednesday, June 7, 2017 2:15 PM

Answers

  • Try the following

    $name = "someone"
    Get-Mailbox -Filter "Extensioncustomattribute4 -eq '$name'"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Heyvoon Wednesday, June 7, 2017 2:48 PM
    Wednesday, June 7, 2017 2:20 PM

All replies

  • Try the following

    $name = "someone"
    Get-Mailbox -Filter "Extensioncustomattribute4 -eq '$name'"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Heyvoon Wednesday, June 7, 2017 2:48 PM
    Wednesday, June 7, 2017 2:20 PM
  • A huge THANK YOU!

    Have been fighting to get this to work for a couple of hours now. Thank you x1000 

    ;-)


    I have a blog! http://unrevealingbox.blogspot.com Where Tips and Tricks comes together to form real walk-through guides.

    Wednesday, June 7, 2017 2:49 PM
  • Heyvoon,

    Your syntax, using curly braces (and no quotes), works in PowerShell V2. Unfortunately, the filter syntax has changed in later versions. Even worse, the help for the AD cmdlets, still document the syntax using only curly braces. In fact the help for Get-ADUser:

    https://technet.microsoft.com/en-us/library/ee617241.aspx

    even has an example using a variable the same way you do:

    $logonDate = New-Object System.DateTime(2007, 1, 1)
    Get-ADUser -filter { lastLogon -le $logonDate }

    But the help About_ActiveDirectory_Filter:

    https://technet.microsoft.com/en-us/library/hh531527%28v=ws.10%29.aspx

    documents the new syntax, but has this example indicating that the variable does not need to be quoted if you use single quotes to enclose the filter:

    $date = (get-date) - (new-timespan -days 5)
    Get-ADUser -Filter 'lastLogon -gt $date'


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Wednesday, June 7, 2017 3:10 PM