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 :
- have an Office365 UserMailbox (should be licensed mailbox)
- 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).
Select-Object mail
→Select-Object -ExpandProperty mail
– user2226112