Announcement

Collapse
No announcement yet.

Filtering Get-Mailbox

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Filtering Get-Mailbox

    I'm trying to write a powershell script that will allow the user to run a "Get-Mailbox" command with a user-defined mailbox attribute and attribute value. The following works:

    Code:
    Get-Mailbox -Filter {CustomAttribute1 -eq 'test'}
    Now I want to make the script look like this:

    Code:
    $AttributeName = "CustomAttribute1"
    $AttributeValue = "test"
    
    Get-Mailbox -Filter {$AttributeName -eq $AttributeValue}
    But I get this error:

    Get-Mailbox : Cannot bind parameter 'Filter' to the target. Exception setting "
    Filter": "Invalid filter syntax. For a description of the filter parameter synt ax see the command help.
    "$AttributeName -eq $AttributeValue" at position 1."
    At line:1 char:20
    + Get-Mailbox -Filter <<<< {$AttributeName -eq $AttributeValue}
    + CategoryInfo : WriteError: ( [Get-Mailbox], ParameterBindingE xception
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Manage ment.RecipientTasks.GetMailbox
    So I tried this:

    Code:
    $AttributeName = "CustomAttribute1"
    $AttributeValue = "test"
    
    $param = @{Filter = '{$AttributeName -eq $AttributeValue}'}
    
    Get-Mailbox @param
    The error was the same. Finally, I tried this:
    Code:
    $AttributeName = 'CustomAttribute1'
    $AttributeValue = 'test'
    
    $sb = [scriptblock]::create("$AttributeName -eq $AttributeValue")
    
    Get-Mailbox -Identity * -Filter $sb
    This time, the error is:
    Get-Mailbox : Cannot bind parameter 'Filter' to the target. Exception setting "
    Filter": "Invalid filter syntax. For a description of the filter parameter synt ax see the command help.
    "customattribute1 -eq test" at position 22."
    At line:1 char:32
    + Get-Mailbox -Identity * -Filter <<<< $sb
    + CategoryInfo : WriteError: ( [Get-Mailbox], ParameterBindingE
    xception
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Manage
    ment.RecipientTasks.GetMailbox
    This one looks promising, since it actually shows me what is in "$sb". I also tried a few other combinations of quotes, curly brackets, and parenthesis with no luck. There must be a way to expand the variables while they're in a hash table. What is it? Thanks.

  • #2
    Re: Filtering Get-Mailbox

    i dont think its appropriate to use a variable name as an attribute... but i am far from a master at scripting.

    from the PHP manual:

    Originally posted by PHP Manual
    "Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' "
    maybe im way off base. idk...

    disregard if i am talking crazy.
    its easier to beg forgiveness than ask permission.
    Give karma where karma is due...

    Comment


    • #3
      Re: Filtering Get-Mailbox

      yes you can only use static values for defining the Get-Mailbox parameters

      Maybe you can try something like this:
      Code:
      $AttributeName = "CustomAttribute1"
      $AttributeValue = "test"
      
      Get-Mailbox -ResultSize Unlimited | where{$_.$AttributeName -eq $AttributeValue}
      OR better, first construct the command containing the search filter and then invoke the command,
      Code:
      $AttributeName = "CustomAttribute1"
      $AttributeValue = "test"
      
      $cmd = "get-Mailbox -Filter {$AttributeName -eq '$AttributeValue'}"
      invoke-expression $cmd
      /Rems
      Last edited by Rems; 24th April 2013, 12:55.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Filtering Get-Mailbox

        Yup, that was it. I put together the $cmd variable as you showed and used Invoke-Expression. Thanks.

        Comment

        Working...
        X