Deleting Disconnected mailboxes from Exchange 2007

By default deleted mailbox retention policy is 30 days and after that deleted mailboxes will be disconnected state for next 30 before its actually gets deleted off the store

Below powershell to get the list of all the users on the specific Exchange server who disconnected mailbox older than 10 days, you can modify the days based on your requirement into the variable $disconnectedUsers

 

$DisconnectedUsers = Get-MailboxStatistics -Server <servername> | where-object { $_.DisconnectDate -ne $null } | ?{$_.DisconnectDate -lt (get-date).AddDays(-10)} | Select DisplayName,Database,MailboxGuid

 

Below command will get the users from variable $DisconnectedUsers one by one in the for loop and delete the same

$DisconnectedUsers | ForEach { Remove-Mailbox -Database $_.Database -StoreMailboxIdentity $_.MailboxGuid -confirm:$false }

Powershell to create Mass Mailboxes in Exchange 2007 from CSV input file

Powershell to create the Mass mailboxes in Exchange 2007 orginisation from CSV input file. Below is the CSV file format which need to have the following header and details in the below mentioned format

Csv Format
Firstname, Lastname,Aliasname,Database,OUPath
Krishna, kumar,krishnakumar,server\storagegroup\store,Users

 

Below is the powershell script to create the mass mailbox by reading the csv file and and create the mailbox. First it take the password as input and reads the csv file and create the mailbox with the information in the csv file

 
$Password=Read-Host “Enter Password” -AsSecureString
Import-CSV C:\CreateNewmailbox.csv |
foreach {
$userprincipalname = $_.Firstname + “.” +  $_.Lastname + “@domain.com”
new-mailbox -name $_.name -alias $_.alias -FirstName $_.Firstname -LastName $_.Lastname -userPrincipalName  $userprincipalname -database $_.Database -OrganizationalUnit  $_.OUpath -Password $Password
}

Powershell to perform Messaging Tracking and Export the Messages details into CSV file

Powershell to perform Messaging Tracking and Export the export the Message details into CSV file

Get-MessageTrackingLog -Sender Krishna.k@domain.com -Start “6/1/09” -End “6/3/09” |select Sender,@{Name=”Recipients”;Expression={$_.recipients}},Recipients,MessageSubject,MessageId,Timestamp| Export-Csv MessagingTracking.csv

Above command can be used to result to the csv file. Multiple option for quering the Messagetrack logs is given below. Modify the query and export the result

Timestamp
ClientIp
ClientHostname
ServerIp
ServerHostname
SourceContext
ConnectorId
Source
EventId
InternalMessageId
MessageId
Recipients
RecipientStatus
TotalBytes
RecipientCount
RelatedRecipientAddress
Reference
MessageSubject
Sender
ReturnPath
MessageInfo

PowerShell to Create New Mailboxes in the smallest database in Exchange 2007 Organization

Loadbalance of the Exchange Database is very important. We need to make sure that  database is not dumped with all the mailbox and once database gets big then move maiboxes to the other database. We can automate this process buy make the script to find the smallest size of the database in the Exchange orginisation and create the mailbox in the same

$MailboxSvr = Get-MailboxServer | select name
$i = 0
foreach($svr in $mailboxsvr)
 {
  $db = Get-MailboxDatabase -Server $svr.Name
  
  foreach($database in $db)
   {
    $Server = $database.Server.Name
    $Db = $database.Identity
    $edbfilepath = $database.EdbFilePath
  
    $path = “`\`\” + $Server + “`\” + $edbfilepath.DriveName.Remove(1) + “$”+ $edbfilepath.PathName.Remove(0,2)
    $Dbsize =  get-item $path |select-object length
    $K = $Server + ” ” + $Db + ” ” + $Dbsize.Length
    if ($i -eq 0 )
     {
     
     $edbsize = $Dbsize.Length
     }
    
    If ($edbsize -gt $Dbsize.Length)
    {
       $edbsize = $Dbsize.Length  
     $sdb = $database.Identity
    } 

    $i = 1
       
   }
    
    
 }
 
Write-output “ENTER THE FOLLOWING DETAILS”
$DName = Read-Host “User Diplay Name  ”
$FName = Read-Host “First Name ”
$LName = Read-Host “Last Name ”
$passwd = Read-Host “Password ” -asSecureString
$PrincipalName = $FName + “.” + $LName + “@domain.com”
$Aliasname = $FName + “.” + $LName
New-Mailbox -Name $DName -Database $sdb -UserPrincipalName $PrincipalName -FirstName $FName -LastName $LName -Alias $Aliasname -Password $passwd -ResetPasswordOnNextLogon $true -SamAccountName $Aliasname

 

You can get the complete copy of the code in the below link file

http://powershell.com/cs/cfs-filesystemfile.ashx/__key/CommunityServer.Components.UserFiles/00.00.00.30.62/NewMailbox_5F00_SmalletDatabase.txt

Executing Exchange Powershell cmdlets and Active directory cmdlets on a single PowerShell

Many times we will have requirement to execute Exchange Comlets and Active Directory(Active Roles) cmdlets on a singlewindow. As we cannot execute exchange cmdlets in  AD shell and vice versa. We have to add snap in to the powershell to execute both cmdlets on a single shell.

1. Need to make sure both Exchange managelent tools and Quest Active role management shell for active directory has been isntalled on the machine
2. Create C:\ExchangeAd.ps1 file with following lines in it

 Add-PSSnapin Quest.ActiveRoles.ADManagement
 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

3. Open Windows powershell and type C:\exchangead.ps1 to add snapin to the windowspowershell. You can execute both the commands on the single shell
4. Or you can Cretae a bat file which contain follwing line and place into your desktop. For Easy accability

       C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -noexit -command C:\ExchangeAd.ps1

5. You can also execute Quest snapin into Exchange powershell or Exchange snapin into Activeroles shell to execute other commands on the same window.

Add-IPAllowListEntry

IP Allow List is used to add the list of IP address into Exchange server to allow computer with IP address to use exchange server for relay purpose and mail sent for the computer which is added in IP Allow list is not scan by anti-spam agent. With this mail comming from the machine with IP allow list will not be sent to spam mails or Junk mail folder. To do this need to make sure that Anti-Spam agent is intalled on the Hub server

Instruction on how install Anti spam agent. Need to make sure you run on all the Hub Transport server

1. Open powershell -> Navigate to the folder “Program Files\Microsoft\Exchange Server\Scripts”
2. Type ./install-AntispamAgents.ps1 and Enter to install the same to get the below output

 

3. Restart the “Microsoft Exchange Transport” service from services
4. Open the Exchange Management Console -> Organization configuration -> select Hub Transport.
5. The “Anti-Spam” tab should be visible in Hub Transport properties. Make sure that IP Allow List is enabled. If not right click and enabled the same

 

Instruction on How to add IP address and IP address range into allow list can be find in the below link.

http://technet.microsoft.com/en-us/library/bb125225(EXCHG.140).aspx