Beginner question concerning filtering – please help

Welcome Forums General PowerShell Q&A Beginner question concerning filtering – please help

Viewing 6 reply threads
  • Author
    Posts
    • #247942
      Participant
      Topics: 2
      Replies: 3
      Points: 11
      Rank: Member

      Dear All,

      I was struggling for hours with some code, which just do not compute.

      I am trying to pass a variable to this cmdlet

      get-mailbox -filter {emailaddresses -like "*@example.com"}

      so I run following code

      $email = "*@example.com"

      Looks the same to me like above command but when I execute it nothing happens

      get-mailbox -filter {emailaddresses -like "$email"}

      Well this is unfair. Why does it not work? Is there a method to find it out? How to make it work?

      I would be deeply grateful, if you would enlighten me 🙂

       

      best regards

      Artur

    • #247957
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      artur, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      Did you try to search for a solution before you came here to ask? 😉

      https://powershell.org/forums/topic/get-mailbox-filter-with-variable-doesnt-work/

    • #248068
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      These filters are black boxes that no one understands how they really work. It’s trial and error. The type of -filter is actually string , not scriptbloock, so it has to get converted to string first. Maybe try this, but I don’t have that command. Something similar works for get-aduser.

      Or this should work. Don’t ask me why one thing doesn’t work with quotes, and the other requires quotes.

      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
    • #248326
      Participant
      Topics: 2
      Replies: 3
      Points: 11
      Rank: Member

      Dear Olaf and js,

       

      Let me express my gratitude for the effort to answer my questions.

      I have read  Read Me Before Posting! You’ll be Glad You Did!. I assume Olaf that you point to the fact that my code was not preformatted. My bad I was looking this option in the menu and did not realised that it is the option in the Paragraph drop-down list.

      Anyhow thank you so much for the link Olaf, this was right direction. js you are also right.

      This works:

       

      This does not work:

       

      Although

      works just fine

       

      Feels bit like WTF, because it feels like this is something which is beyond the rational. Main thing though, I know how to make it work.

       

      Many thanks again guys

      Artur

    • #248797
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      I have found two syntaxes to be the most reliable. As mentioned before, do not use script block notation {} for these filters. The online documentation is incorrect in showing examples of doing so. They are strings and are interpreted as strings.

    • #248836
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      I’d also like to add that it can be confusing especially for those new to powershell who learn variables expand in “” but not ”. That’s not the case for this filter. Also you can add many additional conditions.

      Get-Mailbox -Filter "name -like '$user' -and IsShared -eq '$false' -and IsMailboxEnabled -eq '$true' -and ModerationEnabled -eq '$false'"

      And the powershell rules still apply so you can break it up on the -and operator

      [/crayon]
    • #248962
      Participant
      Topics: 2
      Replies: 3
      Points: 11
      Rank: Member

      AdminOfThings45 and Doug your help and insights I appriciate a lot 🙂

Viewing 6 reply threads
  • You must be logged in to reply to this topic.