This works beautifully:
Get-Mailbox -Filter {DisplayName -like "Axel*"}
Now I want to re-use this in a function. Unfortunately, this construct horribly fails:
> $name = "Axel*"
> Get-Mailbox -Filter {DisplayName -like $name}
I am no powershell god, but I think (!) I have figured out that the closure (I think the {Displayname -eq ...}
is one) is evaluated on execution, which is in the Get-Mailbox
cmdlet, where the variable is no longer available from the outer scope.
So my question is - how do I do that?
I explicitly do not want to do this, because it's about 50 times slower:
Get-Mailbox | Where-Object DisplayName -like $name
Can anyone help me out here?
I found a couple of articles which go into detail, all of them seem to boil down to: "Just use .GetNewClosure()
". That did not work for me:
> $dname = "Axel*"
> Get-Mailbox -Filter {DisplayName -like $dname}.GetNewClosure()
[...still no effect...]