2

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...]
flypenguin
  • 655
  • 2
  • 7
  • 21
  • Did you try `Using:`? This can be used to reference a variable for readonly usage across thread boundaries. It even works in remote PS sessions. `Get-Mailbox -Filter {DisplayName -like $Using:name}` – Moerwald May 14 '19 at 08:37
  • Based on the [Get-Maxilbox] (https://learn.microsoft.com/en-us/powershell/module/exchange/mailboxes/get-mailbox?view=exchange-ps) docu `-Filter` is of type `string` not of type `ScriptBlock`. Are you sure it works with `Get-Mailbox -Filter {DisplayName -like "Axel*"}`? From my point of view it has to be `Get-Mailbox -Filter ({DisplayName -like "Axel*"}).Invoke()`. – Moerwald May 14 '19 at 08:53
  • 1
    I've seen this issue before. Couldnt find the reason, I've assigned whole filter to a variable and it worked. ugly but works `$name = "DisplayName -like 'Axel*'"` `Get-Mailbox -Filter $name` – 2 B May 14 '19 at 08:56
  • 1
    Please see this post https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable **tl;dr** `$dname = "Axel*"` `Get-Mailbox -Filter "DisplayName -like '$name'"` – 2 B May 14 '19 at 11:16
  • @Moerwald - when I try to do `$Using:dname` I get "invalid filter syntax" error. Teh 2nd one also does not work :( – flypenguin May 14 '19 at 16:20
  • 1
    @TeslaGreat - that actually works!! :D Thanks a bunch! – flypenguin May 14 '19 at 16:21
  • The only sad thing is that I still don't know how to do this with closures/script blocks/whatever this is. But it solves my problem :D – flypenguin May 14 '19 at 16:32
  • @flypenguin why do you want to use curly brackets? The link I provided says exactly the opposite - you should "feed" the filter as string :) I didn't know myself why it doesn't work until I tried to help you. Glad that we both benefited from Stack Overflow. – 2 B May 14 '19 at 18:29

1 Answers1

0

Many thanks to Tesla Great above - this solution actually works:

> $dname = "Axel*"
> $full_filter = "DisplayName -like '$dname'"
> Get-Mailbox -Filter $full_filter

And it IS as fast as it should be.

Awesome :)

flypenguin
  • 655
  • 2
  • 7
  • 21