0
votes

We sync our local AD to Office 365.

I have been asked to get the out-of-office reply for users who are:

  1. Disabled
  2. Still have an Exchange mailbox.

I have some of the command but cannot figure out how to make it work:

$disabled = Get-ADUser -SearchBase "ou=Employees,ou=accounts,dc=domain,dc=local" -Filter { UserAccountControl -eq 514 } -Properties mail | Select-Object mail

foreach ($mail in $disabled) {
  Get-MailboxAutoreplyConfiguration -Identity $mail
}
1
Select-Object mailSelect-Object -ExpandProperty mailuser2226112
Which part are you unable to figure out, and what doesnt work with the code. Be more specific.David

1 Answers

0
votes

I believe this can be achieved without the call to AD via Get-ADUser cmdlet to get the list of disabled accounts. You can check the result of Get-Mailbox for the property ExchangeUserAccountControl. If the value is AccountDisabled then the account should be disabled in AD.

So that means you can do this :

    Get-Mailbox -ResultSize Unlimited |
      Where {
        $_.recipienttype -eq "UserMailbox" -and ` # make sure we only get user mailboxes
        $_.recipienttypedetails -eq "UserMailbox" -and ` # make sure we only get licenced mailboxes only, no shared mailboxes, no room mailboxes, etc
        $_.exchangeuseraccountcontrol -like "*accountdisabled*" # make sure we only get disabled user accounts
      } |
      Get-MailboxAutoreplyConfiguration | # we can pipe user mailbox object directly into this cmdlet (no need to go into a foreach loop)
      Format-List identity,autoreplystate,internalmessage,externalmessage # you can remove this and replace with Select then send to Csv or wherever you need

That last line with Format-List is just for viewing (and should be changed if you want to send data to a file, for example), this data can have large output depending if a user has internal or external messages set or not.

Please note that the above will return list of all Active Mailboxes in your Office365 tenant that :

  1. have an Office365 UserMailbox (should be licensed mailbox)
  2. is Disabled in Active Directory (AD account has Enabled : $False)

You can tell if the AutoReply messages are Active by looking at the autoreplystate value. It will either be Enabled or Disabled. So you can even add another Where clause to filter down to only those mailboxes that have autoreplystate : Enabled to only view mailboxes that have active auto replies set (based on your description, this was not clear if it was required or not).