Powershell to get user department from AD and InboxRule from Exchange

Hi,

I would like to prepare a PS script/command to get the Department Name of all users in the Organization with mailboxes, and the InboxRule(s) if exists on an Output file. This is the scratch :

$users2 = (get-mailbox -resultsize unlimited ).SamAccountName

foreach ($user2 in $users2)
{
$user1 = $user2
$user3 = $user2
Get-aduser $user1 -properties * | select-object Department
Get-InboxRule -Mailbox $user3 |Select-Object Identity,Name,Description,Enabled,RedirectTo, MoveToFolder,ForwardTo
}

Inside the loop, if one command, it works. Both, no. If someone can help here will be great.

Thanks so much.

 

Can you provide an example of how you want the output to look?

Thanks, Mike. I am hoping the output will be in a text file. Looks like this :

Display Name : last, first

Department Name : Finance Department

Rule(s) :

.

.

.

 

 

Thanks again.

Anthony

 

Try this. I tested it so, I do know it works.

(Get-Mailbox -ResultSize Unlimited).SamAccountName | 
ForEach {
    ($UserDetail = Get-ADUser -Identity $PSItem -Properties DisplayName, Mail, Department)
    Get-InboxRule -Mailbox $($UserDetail.Mail) | 
    Select-Object Identity,Name,Description,Enabled,RedirectTo, MoveToFolder,ForwardTo
}


# Results

...

Department        : 
DisplayName       : LabUser001 TestUser
DistinguishedName : CN=LabUser001 TestU...
Enabled           : True
GivenName         : LabUser001
Mail              : LabUser001@contoso.com
Name              : LabUser001 TestUser
ObjectClass       : user
ObjectGUID        : e76...
SamAccountName    : LabUser001
SID               : S-1-5...
Surname           : TestUser
UserPrincipalName : LabUser001@contoso.com

Identity     : contoso.com/LabUsers....
Name         : Administrator
Description  : If the message:
                   the message was received from 'Administrator'
               Take the following actions:
                   delete the message
                   and stop processing more rules on this message
               
Enabled      : True
RedirectTo   : 
MoveToFolder : 
ForwardTo    : 

...

To limit the user details to just the values you want, just use calculated properties in the final select.

(Get-Mailbox -ResultSize Unlimited).SamAccountName | 
ForEach {
    $UserDetail = Get-ADUser -Identity $PSItem -Properties DisplayName, Mail, Department
    Get-InboxRule -Mailbox $UserDetail.Mail | 
    Select-Object @{Name='DisplayName';Expression = {$UserDetail.DisplayName}},
    @{Name='Department';Expression = {$UserDetail.Department}},
    Identity,Name,Description,Enabled,RedirectTo, MoveToFolder,ForwardTo
}

# Results

DisplayName  : LabUser001 TestUser
Department   : 
Identity     : contoso.com/LabUsers/LabUser001...
Name         : Administrator
Description  : If the message:
                   the message was received from 'Administrator'
               Take the following actions:
                   delete the message
                   and stop processing more rules on this message
               
Enabled      : True
RedirectTo   : 
MoveToFolder : 
ForwardTo    : 

Thanks so much. I will try both. Thanks again.

Thanks so much. The following error found :

Get-ADUser : Cannot find an object with identity: ‘XXXXXXXX’ under: ‘DC=NNNNNN,DC=com’.
At line:3 char:20

  • ($UserDetail = Get-ADUser -Identity $PSItem -Properties DisplayName, Mail, D …
  • CategoryInfo : ObjectNotFound: (XXXXXXXX:ADUser) [Get-ADUser], ADIdentityNotFoundException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

The following has been set up on the existing shell :

Set-AdServerSettings -ViewEntireForest $true

Thanks again.

Anthony

 

All user have read access to ADDS by default design, so that being set to true is expected.
Use this command by itself using your SamAccountName or other user SamAccountName.

Get-ADUser -Identity 'SomeUserSamAccountName' -Properties DisplayName, Mail, Department

That error simply means the SamAccountName passed in is not known by ADDS.

If you are in a multi-forest / domain environment, then you are in a Windows double Auth hop scenario, and more configuration is required for you to be able to do this. Just do a search for ‘PowerShell double hop’ to get guidance on what it is and some ways to address it.

Thanks so much to all of you and everyone else. Your help is greatly appreciated. I learn a lot from you.

Thanks again.

  1. When opening the output text file. Ran into the space line issue between the 'Description' and 'ForwardTo'.
2. Then tried to do 'export-csv', open Excel to read, got this error : Microsoft.Exchange.Data.Storage.Management.ADRecipientOrAddress[]

I apologize that at the beginning, I mentioned ‘Test file as output’. I should say ‘.csv’. My mistake.

Please help to get around these 2.

Million Helps !!!

Anthony

Hi there,

Wondering if someone can help out on the above, especially the second point ?

Thanks so much.

Thanks everyone. It is the ‘join’. Thanks so much again.