Powershell to apply Fullaccess and Send as permission to Exchange 2007 Mailbox

Below powershell can help to apply users full access rights and and SendAs permission to the mailbox for the given input CSV file

Format ofthe CSV something like this

UserMailbox,User
Krishna,Domain/Krishna

———————————————————————————————————————-

$csv = Import-csv -path “C:\Userdetails.csv”
foreach($line in $csv)
{
Add-MailboxPermission $Line.UserMailbox -AccessRights FullAccess -user $Line.User
Add-ADPermission $Line.UserMailbox -Extendedrights “Send As” -User $Line.User

}

———————————————————————————————————————-

Powershell to get the list of user who last logon time is older then 30 days

Below is the powershell command to get the list of mailbox who last log time is older then 30 days. This would be very help ful when you wanted to try to clean up exchagne server from unused account.  You can change from 30 to 6o or 90 days based on the requirement.

Get-MailboxStatistics | where {$_.Lastlogontime -lt (get-date).AddDays(-30)} | Select displayName,LastLoggedOnUserAccount,LastLogonTime

Managing Deleted Items under Default Managed folders in Exchange 2007

Default Manged folders are managing the default folders which is created by default in every mailbox like Inbox, Deleted Items, Send Items etc. Its Important that we managed some of the folders like Deleted items and sent items to keep storage utilization under control. Below is the step by step to configure to configure Mails in Deleted Items folder which are older than 30 Days

1. Open Exchange management console and click on Orgnisation Mailbox and on the right, click on Managed default folder to get the below snap. This is the list of items in Default managed folder. Same we cam get throught power shell

Get-ManagedFolder

2. Right click Delete Items and click on New Managed content settings and enter the details like show below snap and click on next and new to complete the same. Which alsow to input the details like whats the retention period of the deleted items, what action has to be done , when retention priod reaches. Same can be done with the powershell command

new-ManagedContentSettings -Name ‘Delete_DeletedMails_30Daysold’ -FolderName ‘Deleted Items’ -RetentionAction ‘DeleteAndAllowRecovery’ -AddressForJournaling $null -AgeLimitForRetention ‘30.00:00:00’ -JournalingEnabled $false -MessageFormatForJournaling ‘UseTnef’ -RetentionEnabled $true -LabelForJournaling ” -MessageClass ‘*’ -MoveToDestinationFolder $null -TriggerForRetention ‘WhenMoved’

3. Once we are done this then we have to create Managed mailbox folder policy and added Managed folders(deleted items) and apply the same to the required mailboxes. To do this right click on Mailbox on Microsoft Exchange orginisation  in EMC and in the Result pane click on Managed folder policy and in the Action pane click on “New Managed Folder Default policy” and enter the required details as shown in the below snap and add Deleted Items and click on New to create the same. Same can be done with Powershell command

new-ManagedFolderMailboxPolicy -Name ‘DeletedItems_Retention_Policy’ -ManagedFolderLinks ‘Deleted Items’

 

4. Now need to apply to the individual user by going to the individual mailbox properties from Exchange management console properties ->  Mailbox settings -> double click on Message Records Management and browse and apply the new managed folder maibox policy created and apply the same.  Below is the snap of the same. Same also can be done with powershell command

Set-Mailbox -Identity <username> -ManagedFolderMailboxPolicy “Managed Folder Mailbox Policy Name”

To apply all the mailbox in the Exchange orginisation

Get-Mailbox  |Set-Mailbox -Identity <username> -ManagedFolderMailboxPolicy “Managed Folder Mailbox Policy Name”

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5. Once we apply the policy to the mailboxes we have schedule the policy to run every day.  Right click on the Exchang Mailbox server -> Properties under Server Configuration and click on Messaging Records Management and Customise the required time

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.  You can force this settings to apply for the individual server or mailbox using below mentioned command

Start-ManagedFolderAssistant –Mailbox <Mailbox>
Start-ManagedFolderAssistant –Identity <ServerName>

Script to check if Active Directory User Account is Active or Disabled throught Powershell

Below powershell helps to get if a give account is active or disabled, We can loop it for the given list of users

$K = “Krishna.k”
$adobjroot = [adsi]”
$objdisabsearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
$objdisabsearcher.filter = “(&(objectCategory=user)(objectClass=user)(sAMAccountName= $K)(userAccountControl:1.2.840.113556.1.4.803:=2))”
$resultdisabaccn = $objdisabsearcher.findone()

if($resultdisabaccn)
{
 Write-Output “ACcount is Disabled”
}
 Else
{
 Write-Output “ACcount is Active”
}

Powershell to get Complete Mailbox Statistics in the Exchange 2007 Orginisation

 

Below is the powershell command to export the complete details of the Mailbox in the Exchange Organization. Details like Name, Mailbox size (MB), Mailcount, Mailbox limits, Account Active or disabled, CreatedDate, Lastlogon time, Last logofftime etc into the CSV file. This Data can even imported in to the SQL database and Query to analyst the growth of the mailbox. If you collect these details on Daily basis and uploaded into SQL then you can easily get the details of the mailbox in seconds for auditing or capacity management etc.

With SQL integration you can query details like Top 100 mailbox, Top 100 Fastest growing mailbox, List of all active maibox, List of disabled mailbox, List of mailbox out of default quota limits, maiboxes not used in past few days. Newly created mailbox in past one month etc

Get-Mailbox -ResultSize Unlimited |  select DisplayName, Alias, Database, PrimarySmtpAddress,@{name=’IssuewarningQuota’;expression={if ($_.IssueWarningQuota -match “UNLIMITED”) {“-1”} else {$_.IssueWarningQuota.value.tomb() }}},@{name=’ProhibitSendQuota’;expression={if ($_.ProhibitSendQuota -match “UNLIMITED”) {“-1”} else {$_.ProhibitSendQuota.value.tomb() }}},@{name=’ProhibitSendReceiveQuota’;expression={if ($_.ProhibitSendReceiveQuota -match “UNLIMITED”) {“-1”} else {$_.ProhibitSendReceiveQuota.value.tomb() }}},WhenCreated  |export-csv C:\stats.csv
“DisplayName,Alias,MailboxSizeMB,ItemCount,PrimarysmtpAddress,IssueWarningQuotaMB,ProhibitSendQuotaMB,ProhibitSendReceiveQuotaMB,DatabaseName,CreationDate,LastLogonTime,LastLogoffTime,isActive” | out-file C:\MailstatsResult.csv
$csv = Import-csv -path “C:\stats.csv”
foreach($line in $csv)
{
 $MailboxStats =  Get-MailboxStatistics $Line.Alias | Select TotalItemSize,Itemcount,LastLogoffTime,LastLogonTime
 $L = “{0:N0}” -f $mailboxstats.totalitemsize.value.toMB()
 $Size = “”
 $Len = $L.Split(‘,’)
 for ($i=0; $i -lt $Len.length; $i++)
  {
   $Size = $Size +$Len[$i]
  }
  $temp=$Line.PrimarysmtpAddress
  $adobjroot = [adsi]”
  $objdisabsearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
  $objdisabsearcher.filter = “(&(objectCategory=Person)(objectClass=user)(mail= $Temp)(userAccountControl:1.2.840.113556.1.4.803:=2))”
  $resultdisabaccn = $objdisabsearcher.findone() | select path

if($resultdisabaccn.path)
 {
 $actStatus = “1”
 }
 Else
 {
 $actStatus = “0”
 }
 
 $out =$Line.Displayname  + “,” + $Line.Alias  + “,” +  $Size + “,” + $MailboxStats.ItemCount  + “,” + $Line.PrimarySmtpAddress   + “,” +  $Line.IssueWarningQuota + “,” +  $Line.ProhibitSendQuota  + “,” +  $Line.ProhibitSendReceiveQuota + “,” + $Line.Database + “,” +  $Line.WhenCreated + “,” + $MailboxStats.LastLogonTime + “,” + $MailboxStats.LastLogoffTime + “,” + $actStatus
 $Out | Out-File C:\MailstatsResult.csv -Append
}

You can get the copy of the script in the below text file

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

Count of Mailbox Created in last Week/Month

 

We always for some reason need to find the count of the mailbox created in last week/Month etc. Below is the powershell command to get the count

$Result = Get-Mailbox -resultsize unlimited | where {$_.WhenCreated -gt (get-date).AddDays(-30)} | Select displayName
$Result.Count

To get the details to the text file you can use the below mentioned command to export to the text file

Get-Mailbox -resultsize unlimited | where {$_.WhenCreated -gt (get-date).AddDays(-30)} | Select displayName, Servername,Database, WhenCreated >> Result.txt

Powershell to Get HomeDirectory details for the given list of users

 

Below code helps to get the list of HomeDirectory Path for the given list of users in the text file as input

$UserList = Get-Content “c:\users.txt”
$domain = [ADSI]””
foreach($UserName in $UserList)
{ $searcher = new-object DirectoryServices.DirectorySearcher($domain)
$searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”
$SearchResult = $searcher.findall() | select Properties
$Homepath = $username + ” –  ” + $SearchResult.properties.homedirectory
$Homepath
}

Working with Recovery Storage Group in Exchange 2007 using powershell and Microsoft Exchange Troubleshooting Assiatance

 

Recovery Storage group is the feature of restoring and mount the second copy of the exchange mailbox store to the exchange server to export mails and data from the required mailboxes with out affecting the production. This is originally introduced in Exchange 2003 SP1.

 

Features

  1. Only one Recovery storage group can be created in a exchange server
  2. Recovering database and Exchange 2007 server should be on the same active directory forest.
  3. Recovery Storage group can be managed in two ways one with Microsoft Exchange troubleshooting assistant(ExTRA) and Exchange power shell    

Below article describes procedure steps to work on Recovery Storage group using power shell

1. Below snap of DB3 which we will be working on recovering in to the Recovery Storage Group

 

 

 

2. Lets take the backup of DB3 using default windows Backup Utility

 

 

 

 

3. Create a new Recovery Storage group using Exchange powershell, below is the command for the same. –Recovery option will create a recovery storage group.

New-StorageGroup –Server <servername>  –LogFolderPath “D:\Recovery Storage Group\log” –Name “Recovery Storage Group” –SystemFolderPath “D:\Recovery Storage Group\log” –Recovery

 

4. Get-StorageGroup “Recovery storage group” | fl
   Will get the details of the new Recovery storage group which is created now

5. Create the new Database with the name DB3 and provide the path of the Edb file. 
  

5. Create the new Database with the name DB3 and provide the path of the Edb file.
   New-MailboxDatabase –MailboxDatabaseToRecover “DB3” –StorageGroup “Servername\Recovery Storage Group” –EDBFilePath “D:\Recovery Storage Group\DB\DB3.edb”

6. Once Mailbox Database is created we need to restore from the backup before that the restore we need set AllowFilerestore to true with the below command
    Set-MailboxDatabase -Identity “<ServerName>\Recovery Storage Group\DB3” -AllowFileRestore $true

7. Use Backup Utility to restore the Database to the Recovery Storage group. By selecting the DB3 and click restore and make sure that “Last Restore Set” is checked

 

 

 

 

 

 

 

 

 

 

 

 

8. Mount the Restored database using below mentioned power shell command
   Mount-Database –Identity “<SERVERNAME>\Recovery Storage Group\DB3”

9. Final step is merging mails from the Recovery Storage group mails to production mailbox. This can be done either both from ExTRA (Exchange Trouble shooting assistance) and from Power shell. I feel that recovering mail using Extra is much easier then recovering through power shell. Power shell will not only merge on the production mailbox into the folder name Recovered but ExTRA will directly merge into the production mailbox.  

10. To open ExTRA, Open Exchange Management console and click on tools and on the right panel click on Database Recovery Management.

 

 

 

11. Click on “Select Task” on the left column on ExTRA and click on Database recovery Management and  In the next page enter the Mailbox Server name and DC and click on  Next to continue to get the below mentioned page and click on Merge or copy mailbox content

 

 

 

 

 

 

 

 

 

 

 

 

12. Click on “Gather merge information”

 

 

 

 

13. Select the required mailbox and click on “Perform merge action” to merge the recovered mailbox to the production mailbox. Recovered mail will directly reflect in the mailbox of the user

Converting Legacy Mailbox to User Mailbox in Exchange 2007

 

During migration of users from Exchagne 2003 to exchagne 2007  some time you may see like after user is migrated to exchange 2007 server and if you see the properties of the user , it will still show as Legacy mailbox.  If user is still showing as Legacy Mailbox then chances that OWA may not work for that user. User has to be converted to the regular mailbox. Below is the powershell to set legacy mailbox to user mailbox.

Set-Mailbox -id <Username> -ApplyMandatoryProperties