Find accounts that don't have a certain EmailAddress/proxyAddress

This topic contains 10 replies, has 4 voices, and was last updated by  Dan Potter 10 months, 1 week ago.

  • Author
    Posts
  • #60012

    Blake Fletcher
    Participant

    Hi, we have a hybrid Exchange setup and we're trying to find all mailboxes that don't have 2 smtp email addresses / proxy addresses. I can check it on either the Exchange server or a DC, whichever would be easier, but below is what I've been trying.

    Get-Mailbox -OrganizationalUnit "TEST OU" | ?{$_.EmailAddresses -like "*domain.mail.onmicrosoft.com*"}
    

    This works fine in listing the mailboxes that have it assigned, but I need to find the ones that don't. It's a Monday and I can't think of how to do the reverse, would I have to use a "If..Then" statement or something?

    Also, there should be two smtp addresses, domain.onmicrosoft.com and domain.mail.onmicrosoft.com, so I need to search and make sure both are there.

  • #60015

    Don Jones
    Keymaster

    Use -notlike instead of -like?

    • #60016

      Blake Fletcher
      Participant

      Tried that, it still lists everyone in the OU. Tried it with an * and without, same thing.

  • #60019

    Don Jones
    Keymaster

    Are the "wrong" people in possession of a blank address? And I'm thinking that the field is actually a collection capable of holding multiple values – which means -notlike wouldn't work. You'd need a -notcontains or something, but those don't do wildcards. You might end up just enumerating every user and running through a ForEach to examine each one. I'm not sure AD is going to be able to filter this server-side for you.

  • #60021

    Blake Fletcher
    Participant

    Yeah the EmailAddresses property from Get-Mailbox can contain multiple addresses (X500, smtp, etc.) and each user should have a few already because of an email address policy. I tried it with -notcontains as well, still no luck 🙁

  • #60022

    Dan Potter
    Participant

    Is there one mailbox that you know you can test against? If like works notlike should also work. I don't have any that are missing.

    get-mailbox -Filter {emailaddresses -notlike "*microsoft.com"}

    get-mailbox -Filter {emailaddresses -like "*microsoft.com"} | select name,alias,servername,@{n='emailaddys';e={($_.emailaddresses | ?{$_ -match 'microsoft'})-join '; '}}

    • #60034

      Blake Fletcher
      Participant

      Just the one line:

      get-mailbox -Filter {emailaddresses -notlike "*microsoft.com"}

      is working, thanks!!!

  • #60040

    Dan Potter
    Participant

    np.

  • #60042

    Blake Fletcher
    Participant

    Wonder why the -notlike filter works before the pipe, but not after? Weird.

  • #60043

    Ron
    Participant

    This is a bit too complicated for a one liner. Emailaddresses is a collection of objects. It would probably be easier to write a function to test them.

    function checksmtp {
      param($adr)
      
      $found1 = $false
      $found2 = $false
      foreach ($a in $adr) {
        if ($a.prefixstring -eq 'smtp') {
          if ($a.addressstring -like "*dom1.com") {
            $found1 = $true
          }
          if ($a.addressstring -like "*dom2.com") {
            $found2	= $true
          }
        }
      }
      ($found1 -and $found2)
    }
    
    Get-Mailbox -OrganizationalUnit "TEST OU" | ? {-not (checksmtp($_.emailaddresses))}
  • #60051

    Dan Potter
    Participant

    It is a multi-valued property.

    get-mailbox | ?{($_.emailaddresses -join ';') -notmatch 'microsoft'}
    get-mailbox | ?{($_.emailaddresses -join ';') -match 'microsoft'}

You must be logged in to reply to this topic.