# encoding: ascii
# api: powershell
# title:
# description: journal mailboxes
# version: 0.1
# license: CC0
# x-poshcode-id: 2418
# x-archived: 2010-12-24T06:51:37
#
#
# Load the Exchange 2007 snap-in if they are not already loaded
Add-PSSnapIn -Name Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue
# Find all databases (except sxmb201\sg01\db01 Cisco Unity System Mailboxes are stored here. Don't monkey with it.)
$databases = Get-MailboxDatabase | Where-Object {$_.Identity -notlike "sxmb201\sg01\db01"}
# Loop through each of the databases
foreach ($database in $databases)
{
# Get the database size summing the size of all mailboxes and "Deleted Items" in the database
$dbsize = (Get-MailboxStatistics -Database $database.Identity | Measure-Object -Property TotalItemSize,TotalDeletedItemSize -Sum).Sum
# Compare the sizes to find the smallest DB
if (($dbsize -lt $smallestdbsize) -or ($smallestdbsize -eq $null))
{
$smallestdbsize = $dbsize
$smallestdb = $database
}
}
Write-output "Enter Logon account of user to Journal"
$UserToJournal = Read-Host "Mailbox to Journal"
$RuleName = 'Journal-'+$UserToJournal
$Password = convertto-securestring "Password123" -asplaintext -force
$UPN = $RuleName + "@domain.pri"
$MBX = $UserToJournal + "@domain.pri"
$PrimarySMTP = (get-mailbox $MBX).PrimarySmtpAddress.ToString()
# Create a new mailbox that data will be journaled to
New-Mailbox -Name $RuleName -Database $database -UserPrincipalName $UPN -FirstName $RuleName -Alias $RuleName -Password $Password -ResetPasswordOnNextLogon $False -SamAccountName $RuleName -OrganizationalUnit 'kfbdom1.kyfb.pri/Exchange/ServiceMailboxes' -DomainController sdom201.domain.pri
# Sleep a few seconds for replication so there are no errors during Journal Rule creation
Write-Host "Please wait for replication (10 seconds)"
Start-Sleep -s 10
# Create Journal Rule
New-JournalRule -Name $UserToJournal -JournalEmailAddress $RuleName -Scope 'Global' -Enabled $true -Recipient $PrimarySMTP -DomainController sdom201.domain.pri
# Hide from the GAL for obvious reasons
Set-Mailbox $RuleName -HiddenFromAddressListsEnabled $true -DomainController sdom201.domain.pri
# Send email with info to access new mailbox
$MailBod = @"
The Journaling mailbox for this user can be accessed at
http://weboutlook.domain.com
Username = $RuleName
Password = Password123
Any mail from this date going forward (Sent and Received) will be placed in the Inbox at this location. I have exported all available existing mail to a folder called PreviousData.
Let me know if you have any questions or problems accessing the information.
Thanks,
Aaron Anderson
"@
$MailSub = "Journaled Mailbox Information"
$MailSMTP = "kyfbsmtp.domain.pri"
Send-MailMessage -To "aaron@domain.com" -Subject $MailSub -From "aaron@domain.com" -body $MailBod -SmtpServer $MailSMTP
# Export any existing mailbox data to the new Journal mailbox
Export-Mailbox $MBX -TargetMailbox $RuleName -TargetFolder PreviousData -Confirm:$false