Powershell to get user Mailbox Logon statistics

Powershell to get user Mailbox Logon statistics

Some times you may need to take the user mailbox access statistics. Get-mailboxstatistics will only gives the last logon time and last logoff time. If you wanted to find the details for a week then you need to enable to logging level and trace the event ID: 1016 in the Application logs.

Below powershell command helps you to do the same

Set-EventLogLevel “MSExchangeIS\9000 Private\Logons” –level High

Exchange 2007 Self-Signed Certificate

Exchange 2007 will issue Self-Signed to all except Mailbox Server. Self-Signed certificates are only valid for one year.
Below command helps to create Self-Singed for SMTP TLS connection. You can use the below command when you get this error in transport server

“The STARTTLS certificate will soon expire. The STARTTLS certificate is the certificate not used for internal TLS applications” Event id :12023

 

New-ExchangeCertificate -PrivateKeyExportable $TRUE -SubjectName “CN=servername” -DomainName FQDNservername |Enable-ExchangeCertificate -Services SMTP

 

Below command help to renew the exisitng self-signed certificate to next one year from the date of running this command

 

Get-ExchangeCertificate | New-ExchangeCertificate

Powershell to Customize Exchange 2007 Online Maintenance

Powershell to customize online maintenance schedule. During this interval, tasks like dumpster cleanup (the deletion of messages that have passed their deleted item retention date), deleted mailbox cleanup, and online defragmentation (database objects that are no longer being used are detected and removed, thereby making additional database space available) are performed. These processes keep the Exchange Server computer healthy and the performance stable. Online maintenance should be set to run at least four hours a day; failing to do so will cause performance degradation over time.

Get-MailboxDatabase | Set-MailboxDatabase -MaintenanceSchedule “Sun.1:00 PM-Sun.5:00 PM”,”Mon.1:00 PM-Mon.5:00 PM”, “Tue.1:00 PM-Tue.5:00 PM”, “Wed.1:00 PM-Wed.5:00 PM”,”Thu.1:00 PM-Thu.5:00 PM”, “Fri.1:00 PM-Fri.5:00 PM”,”Sat.1:00 PM-Sat.5:00 PM”

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.