Sunday, February 09, 2014

Advanced Topic - Useful Exchange PowerShell aka Management Shell Commands

If you have used a version of Microsoft Exchange later than 2003, you already know many of the built-in GUI tools were replaced by Management Shell cmdlets with a prompt of PS> for PowerShell.  In other words, many of the things you used to be able to accomplish with the GUI now need to be run with Microsoft's Management Shell.  This requires a firm understanding of what cmdlets, parameters, and syntax are available to you.  Listed below are some helpful cmdlets that will make your life easier if you are responsible for supporting or administering Exchange.  Note:  All cmdlet examples listed below assume you have the path to the databases you want to run the commands against typed before the cmdlets.  Also, do not use double quotes in the commands as shown below.  The double quotes represent a variable you will need to replace with your specific data.  Single quotes as shown below are necessary around strings of text with spaces or special characters.

Users


1.  Moving a User's Mailbox from One Database to Another
When Exchange is initially administered there are often only a few mailbox databases created.  However, as time goes on and your database sizes increase, it can become valuable to create additional mailbox databases to spread users across.  Note:  If you have the Standard version of Exchange you are limited to only 5 mailbox databases and Public Folders counts as one.  With the Enterprise version of Exchange you can create unlimited mailbox databases.

     PS> New-MoveRequest -Identity " 'username@domain.com' " -TargetDatabase "Destination Mailbox Database Name"

2.  Moving a User's Mailbox that has Greater than 50 Bad Emails
If you use the wizard to move a user's mailbox and they have bad items (corrupted emails, etc.), their mailbox move will fail.  You can restart the move and change the bad item limit in the wizard to move the mailbox, but if you enter a number larger than 50, even though the wizard allows it, the move will fail.  Instead, use this:


     PS> New-MoveRequest -Identity " 'username@domain.com' " -PrimaryOnly -TargetDatabase "Destination Mailbox Database Name" -BadItemLimit "200 (or whatever number you choose)" -AcceptLargeDataLoss

3.  Repairing the Mailbox Folder View for a Single Mailbox
After a mailbox has been moved or recovered, there are times when the folder structure view is no longer available.  If you look at the user's mailbox size you will see all of their email but the user is unable to see them in one view or another.  For example, the folders might be able to be viewed using Outlook webmail but not in Outlook on the user's local computer.  To detect and repair this: 

     PS> New-MailboxRepairRequest -Mailbox " 'username@domain.com' " - CorruptionType FolderView

4.  Move Status of a Single Mailbox
     PS> Get-MoveRequest -Identity " 'username@domain.com' "

5.  Move Statistics for a Single Database
     PS> Get-MoveRequestStatistics -MoveRequestQueue "Mailbox Database Name"

6.  Move Request Statistics for All Mailboxes being Moved
     PS> Get-MoveRequest | Get-MoveRequestStatistics

Management

7.  Mailbox Statistics - Specific User
     PS> Get-MailboxStatistics -Identity "User Alias" or " 'username@domain.com' "

8.  Mailbox Statistics - Mailbox Database
     PS> Get-MailboxStatistics -Database "Mailbox Database Name"

9.  Mailbox Statistics - Server
     PS> Get-MailboxStatistics -Server "Server Name"

Maintenance

10.  Determining the Last Successful Backup Date for a Mailbox Database

     PS> Get-MailboxDatabase -Identity "Mailbox Database Name" -Status | Ft name, LastFullBackup

11.  Forcing Deleted Mailboxes to Show Up as Disconnected
There are times when a user's mailbox does not show up as disconnected in Exchange even though it has been deleted.  Although a deleted mailbox normally shows up as disconnected immediately, this may not occur until database maintenance for the database runs.  To run the database maintenance and force the mailbox to appear disconnected:

     PS> Clean-MailboxDatabase "Mailbox Database Name"

Global

12.  Adding Users to a Distribution List in Bulk
When you add a large group of users at once it can be helpful to add them to distribution lists in bulk.  An example of this would be working at a school with a new set of teachers at the beginning of each school year.  Adding them as members of the teacher distribution list in bulk, instead of going into each new user's AD account, is much quicker. Create a unique list to import listing all new users for each distribution list.  Once created, use this:

     PS> Get-Content "path to file\file.txt" | Add-DistributionGroupMember  "Display name of Distribution Group"

13.  Creating an Email Relay
When you need to create a relay for a device or software needing access to Exchange, you will need to set up a relay on Exchange.  An example of when this is necessary would be with an all-in-one copier that can scan to email or scan to a user's desktop.  Another example would be software like student information systems or fundraising software that links to Exchange to send emails to parents directly from templates within the software.

To create a new receive connector (relays were created on the SMTP virtual server with an assigned IP address in Exchange 2003):

     PS> New-ReceiveConnector -Name "Anonymous Relay" -Usage Custom -PermissionGroups AnonymousUsers -Bindings x.x.x.x:25 -RemoteIpRanges y.y.y.y

The x.x.x.x represents the IP address of Exchange server to bind to, or use 0.0.0.0 for all available IP's.  The y.y.y.y represents the IP address of the device needing to relay email to Exchange, as in the copier or device with special software.  Note:  The sending device needs to have a static IP address assigned to it for this to continue to work.

     PS> Get-ReceiveConnector "Anonymous Relay" | Add-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" -ExtendedRights "Ms-Exch-SMTP-Accept-Any-Recipient"

This grants permission to the receive connector to relay email outside of Exchange as the default is to relay to mailboxes only within Exchange.

As you can see there are many options with the cmdlets and endless ways to gather the data you need.  While using the cmdlets may not seem intuitive at first, you will find there are times when it is the only way to gather the accurate information you need.  Lastly, visit Microsoft's website for more examples of cmdlets and their uses.

Enjoy this post? Subscribe to our Blog

No comments:

Post a Comment