itant
April 12, 2019, 10:38am
1
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?
itant
April 12, 2019, 2:09pm
3
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 :
itant
April 12, 2019, 8:57pm
5
Thanks so much. I will try both. Thanks again.
itant
April 12, 2019, 9:22pm
6
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.
itant
April 13, 2019, 11:09pm
8
Thanks so much to all of you and everyone else. Your help is greatly appreciated. I learn a lot from you.
Thanks again.
itant
April 15, 2019, 10:07pm
9
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
itant
April 17, 2019, 7:48am
10
Hi there,
Wondering if someone can help out on the above, especially the second point ?
Thanks so much.
itant
April 17, 2019, 1:33pm
11
Thanks everyone. It is the ‘join’. Thanks so much again.