Reputation: 13
I'm trying to generate a mailbox report for our hosted Exchange platform and automate things a bit. Basically each tenant is in an OU. So I'm trying to first pull a list of OU's, then count the mailboxes in each OU. Here's what I have so far:
$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })
foreach ($ou in $ous) {
(Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count
}
It works... sort of. The result will be a ton of numbers on each line, a mailbox count for each OU. Problem is, then I have OU's in the $ous variable, and I'm outputting the count to the screen. What I need is output two columns, the OU, alongside the count in another column, so I can pipe it into Export-CSV cmdlet so I can have client name (OU), and the count in a CSV file to them email.
I'm just not sure how to get that combination of data all at once.
Upvotes: 0
Views: 597
Reputation: 46
If I'm understanding you correctly, the answer is very simple. This will output csv content. If you'd prefer, you can drop the quotes and replace the comma with a `t, then you can copy/paste from the PowerShell console into Excel.
$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })
foreach ($ou in $ous) {
'"' + $ou.Name + '",' + (Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count
}
Upvotes: 0
Reputation: 2149
Easiest way to organise information is to put it into an object, if you need to alter the data later on it's already in an object that you can manipulate.
$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })
foreach ($ou in $ous)
{
# This is how many mailboxes this OU has
$mailboxCount = (Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count
# This is a fancy object to store our information
$mailboxObject = New-Object psobject -Property ([Ordered]`
@{
"OU" = $ou
"MailboxCount" = $mailboxCount
})
# You can export your CSV here or do other stuff
# $mailboxObject | Export-CSV C:\MyPath -NoTypeInformation
}
Small note: If you're not using PowerShell v3 take out the [Ordered]
attribute :)
Upvotes: 0