Filtering Get-Mailbox

Home Forums Messaging Software Exchange 2007 / 2010 / 2013 Filtering Get-Mailbox

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    mhashemi
    Member
    #161413

    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:

    Get-Mailbox -Filter {CustomAttribute1 -eq ‘test’}[/CODE]

    Now I want to make the script look like this:

    [CODE]$AttributeName = “CustomAttribute1”
    $AttributeValue = “test”

    Get-Mailbox -Filter {$AttributeName -eq $AttributeValue}[/CODE]

    But I get this error:

    [QUOTE]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[/QUOTE] So I tried this: [CODE]$AttributeName = "CustomAttribute1" $AttributeValue = "test" $param = @{Filter = '{$AttributeName -eq $AttributeValue}'} Get-Mailbox @param [/CODE] 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[/CODE] This time, the error is: [QUOTE]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[/QUOTE] 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.[CODE]Get-Mailbox -Filter {CustomAttribute1 -eq 'test'}[/CODE] Now I want to make the script look like this: $AttributeName = “CustomAttribute1” $AttributeValue = “test” Get-Mailbox -Filter {$AttributeName -eq $AttributeValue}[/CODE] But I get this error: [QUOTE]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[/QUOTE] So I tried this: [CODE]$AttributeName = "CustomAttribute1" $AttributeValue = "test" $param = @{Filter = '{$AttributeName -eq $AttributeValue}'} Get-Mailbox @param [/CODE] 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[/CODE] This time, the error is: [QUOTE]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[/QUOTE] 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.[CODE]$AttributeName = "CustomAttribute1"
    $AttributeValue = “test”

    Get-Mailbox -Filter {$AttributeName -eq $AttributeValue}[/CODE]

    But I get this error:

    Quote:
    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:

    $AttributeName = “CustomAttribute1”
    $AttributeValue = “test”

    $param = @{Filter = ‘{$AttributeName -eq $AttributeValue}’}

    Get-Mailbox @param
    [/CODE]

    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[/CODE]

    This time, the error is:
    [QUOTE]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[/QUOTE] 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.[CODE]$AttributeName = "CustomAttribute1"
    $AttributeValue = “test”

    $param = @{Filter = ‘{$AttributeName -eq $AttributeValue}’}

    Get-Mailbox @param
    [/CODE]

    The error was the same. Finally, I tried this:
    $AttributeName = ‘CustomAttribute1’
    $AttributeValue = ‘test’

    $sb = [scriptblock]::create(“$AttributeName -eq $AttributeValue”)

    Get-Mailbox -Identity * -Filter $sb[/CODE]

    This time, the error is:
    [QUOTE]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[/QUOTE] 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.[CODE]$AttributeName = 'CustomAttribute1'
    $AttributeValue = ‘test’

    $sb = [scriptblock]::create(“$AttributeName -eq $AttributeValue”)

    Get-Mailbox -Identity * -Filter $sb[/CODE]

    This time, the error is:

    Quote:
    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.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.