Running Discovery periodically on Exchange 2010 SP1

Published Nov 29 2010 04:46 PM 4,387 Views

 

SearchMailboxes.ps1 is a script to run a discovery search (using Exchange 2010's Multi-Mailbox Search) periodically on a set of mailboxes. The script works only with Microsoft Exchange 2010 SP1. Please note, Multi-mailbox Search is an Enterprise CAL feature.

Who needs it: A compliance officer who runs discovery searches periodically to look for messages containing specific keywords. It makes life easier by automating the process of running a discovery search at fixed intervals using a scheduled task. Once the script is deployed the compliance officer can log on to the Exchange Control Panel (ECP) and select the particular date for which he/she wants to get search results.

Here is the snapshot of the ECP after running the script weekly:

How the script works: This script runs a discovery search on the specified mailboxes over a period of time. To learn more about discovery, see Understanding Multi-Mailbox Search.

For the first time the search is run, you can specify the start date in the search.config file. The current date is automatically used as the end date. For subsequent searches, the start date is automatically obtained from the previous search. If the previous search is successful, its end date is used as the start date for the new search. If the previous search failed, the start date specified in that search is used as the start date for the new search. This ensures that you don't miss any messages due to a failed search. You can set the frequency at which the script is run by using the Task Scheduler.

Before running the script, make sure that the user account you use to run it is added to the Discovery Management RBAC role group. See Add a User to the Discovery Management Role Group for instructions on how to do this.

The package contains the following files:

  1. Setup.bat: This creates a scheduled task using the Schtasks command. It's configured to run once a week (on Monday). You can edit the file to configure the frequency at which you want to run the script. You can also modify the scheduled task using the Task Scheduler - see Schedule a Task for more info.
  2. DailyMailboxSearch.bat: This is the batch file that the schedule task runs. Edit it to specify the path to exshell.psc1 (the default path is \Program Files\Microsoft\Exchange Server\V14\Bin\). To run a Shell (EMS) script on a schedule using the Windows Task Scheduler, you must call the Shell and include the script that you want to run as a parameter. For more details, see Scripting with the Exchange Management Shell.

    Add the mailboxes to search

    powershell -PSConsoleFile "d:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1" -command ".\SearchMailboxes.ps1 "

    • # Name of the mailboxes accepts all the types of mailbox names that can be specified to get-mailbox cmdlet
    • # ex: SearchMailboxes.ps1 user1
    • # ex: SearchMailboxes.ps1 user*
    • # ex: SearchMailboxes.ps1 *user*
  3. Search.Config: Open Search.Config and edit the value of Discovery mailbox. This command gets the Discovery mailbox:

    Get-Mailbox -Identity Discovery*

    Edit the start date to go back number of days to get the first search results.
    Edit the keywords and separate them with the conjunction 'OR'
    This is a mandatory file and it needs to be present in the same location as SearchMailboxes.ps1.

  4. LastSearch.txt: This file is generated when the SearchMailboxes.ps1 script is run for the first time. It stores the information about the last time the search was run. If this file isn't present, the SearchMailboxes.ps1 script will pick the start date of the search from the Search.config file.
  5. SearchMailboxes.ps1: This is the script that consumes search.config and lastSearch.txt to generate the search results. It's not required to modify this script because all configurable parameters are contained in the other files in this list.

Download the script: You can download this script and all of the associated files as attachments to this blog post.

EDIT 11/30/2010: Added a note that the multi-mailbox search is an Enterprise CAL feature.

Kunal Nikam 

9 Comments
%3CLINGO-SUB%20id%3D%22lingo-sub-588807%22%20slang%3D%22en-US%22%3ERunning%20Discovery%20periodically%20on%20Exchange%202010%20SP1%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-588807%22%20slang%3D%22en-US%22%3E%3CP%3E%3C%2FP%3E%3CDIV%20class%3D%22ehlopost%22%3E%0A%3CP%3E%3CSPAN%20class%3D%22filename%20bold%22%3ESearchMailboxes.ps1%3C%2FSPAN%3E%20is%20a%20script%20to%20run%20a%20discovery%20search%20(using%20Exchange%202010's%20%3CSPAN%20class%3D%22bold%22%3EMulti-Mailbox%20Search%3C%2FSPAN%3E)%20periodically%20on%20a%20set%20of%20mailboxes.%20The%20script%20works%20only%20with%20Microsoft%20Exchange%202010%20SP1.%20Please%20note%2C%20Multi-mailbox%20Search%20is%20an%20Enterprise%20CAL%20feature.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22bold%22%3EWho%20needs%20it%3A%3C%2FSPAN%3E%20A%20compliance%20officer%20who%20runs%20discovery%20searches%20periodically%20to%20look%20for%20messages%20containing%20specific%20keywords.%20It%20makes%20life%20easier%20by%20automating%20the%20process%20of%20running%20a%20discovery%20search%20at%20fixed%20intervals%20using%20a%20scheduled%20task.%20Once%20the%20script%20is%20deployed%20the%20compliance%20officer%20can%20log%20on%20to%20the%20Exchange%20Control%20Panel%20(ECP)%20and%20select%20the%20particular%20date%20for%20which%20he%2Fshe%20wants%20to%20get%20search%20results.%3C%2FP%3E%0A%3CP%3EHere%20is%20the%20snapshot%20of%20the%20%3CACRONYM%20title%3D%22Exchange%20Control%20Panel%22%3EECP%3C%2FACRONYM%3E%20after%20running%20the%20script%20weekly%3A%3C%2FP%3E%3CIMG%20src%3D%22http%3A%2F%2Fblogs.technet.com%2Fthemes%2Fexchange%2Fimages%2F457070_ExchangeDiscoveryJob01.jpg%22%20%2F%3E%3CP%3E%3CSPAN%20class%3D%22bold%22%3EHow%20the%20script%20works%3A%3C%2FSPAN%3E%20This%20script%20runs%20a%20discovery%20search%20on%20the%20specified%20mailboxes%20over%20a%20period%20of%20time.%20To%20learn%20more%20about%20discovery%2C%20see%20%3CSPAN%20class%3D%22bold%22%3E%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fdd335072.aspx%22%20title%3D%22Go%20to%20'Understanding%20Multi-Mailbox%20Search'%20in%20Exchange%202010%20docs%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EUnderstanding%20Multi-Mailbox%20Search%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FP%3E%0A%3CP%3EFor%20the%20first%20time%20the%20search%20is%20run%2C%20you%20can%20specify%20the%20start%20date%20in%20the%20%3CSPAN%20class%3D%22command%20lightyellow%22%3Esearch.config%3C%2FSPAN%3E%20file.%20The%20current%20date%20is%20automatically%20used%20as%20the%20end%20date.%20For%20subsequent%20searches%2C%20the%20start%20date%20is%20automatically%20obtained%20from%20the%20previous%20search.%20If%20the%20previous%20search%20is%20successful%2C%20its%20end%20date%20is%20used%20as%20the%20start%20date%20for%20the%20new%20search.%20If%20the%20previous%20search%20failed%2C%20the%20start%20date%20specified%20in%20that%20search%20is%20used%20as%20the%20start%20date%20for%20the%20new%20search.%20This%20ensures%20that%20you%20don't%20miss%20any%20messages%20due%20to%20a%20failed%20search.%20You%20can%20set%20the%20frequency%20at%20which%20the%20script%20is%20run%20by%20using%20the%20Task%20Scheduler.%3C%2FP%3E%0A%3CP%3EBefore%20running%20the%20script%2C%20make%20sure%20that%20the%20user%20account%20you%20use%20to%20run%20it%20is%20added%20to%20the%20%3CSPAN%20class%3D%22bold%22%3E%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fdd351080.aspx%22%20title%3D%22More%20about%20the%20'Discovery%20Management'%20role%20group%20in%20Exchange%202010%20docs%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EDiscovery%20Management%3C%2FA%3E%3C%2FSPAN%3E%20%3CACRONYM%20title%3D%22Role%20Based%20Access%20Control%22%3ERBAC%3C%2FACRONYM%3E%20role%20group.%20See%20%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fdd298059.aspx%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EAdd%20a%20User%20to%20the%20Discovery%20Management%20Role%20Group%3C%2FA%3E%20for%20instructions%20on%20how%20to%20do%20this.%3C%2FP%3E%0A%3CP%3EThe%20package%20contains%20the%20following%20files%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%20class%3D%22filename%20bold%22%3ESetup.bat%3C%2FSPAN%3E%3A%20This%20creates%20a%20scheduled%20task%20using%20the%20%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fcc772785(WS.10).aspx%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3ESchtasks%3C%2FA%3E%20command.%20It's%20configured%20to%20run%20once%20a%20week%20(on%20Monday).%20You%20can%20edit%20the%20file%20to%20configure%20the%20frequency%20at%20which%20you%20want%20to%20run%20the%20script.%20You%20can%20also%20modify%20the%20scheduled%20task%20using%20the%20Task%20Scheduler%20-%20see%20%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fcc748993.aspx%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3ESchedule%20a%20Task%3C%2FA%3E%20for%20more%20info.%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22filename%20bold%22%3EDailyMailboxSearch.bat%3C%2FSPAN%3E%3A%20This%20is%20the%20batch%20file%20that%20the%20schedule%20task%20runs.%20Edit%20it%20to%20specify%20the%20path%20to%20%3CSPAN%20class%3D%22command%20lightyellow%22%3Eexshell.psc1%3C%2FSPAN%3E%20(the%20default%20path%20is%20%3CSPAN%20class%3D%22filepath%22%3E%5CProgram%20Files%5CMicrosoft%5CExchange%20Server%5CV14%5CBin%5C%3C%2FSPAN%3E).%20To%20run%20a%20Shell%20(%3CACRONYM%20title%3D%22Exchange%20Management%20Shell%22%3EEMS%3C%2FACRONYM%3E)%20script%20on%20a%20schedule%20using%20the%20Windows%20Task%20Scheduler%2C%20you%20must%20call%20the%20Shell%20and%20include%20the%20script%20that%20you%20want%20to%20run%20as%20a%20parameter.%20For%20more%20details%2C%20see%20%3CA%20href%3D%22http%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fbb123798.aspx%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EScripting%20with%20the%20Exchange%20Management%20Shell%3C%2FA%3E.%3CH3%20id%3D%22toc-hId-1621036121%22%20id%3D%22toc-hId-1139047431%22%3EAdd%20the%20mailboxes%20to%20search%3C%2FH3%3E%0A%3CP%20class%3D%22code%22%3Epowershell%20-PSConsoleFile%20%22d%3A%5CProgram%20Files%5CMicrosoft%5CExchange%20Server%5CV14%5CBin%5Cexshell.psc1%22%20-command%20%22.%5CSearchMailboxes.ps1%3CNAME%3E%3C%2FNAME%3E%3CNAME%3E%3C%2FNAME%3E%22%3C%2FP%3E%0A%3CUL%20class%3D%22nobullet%22%3E%0A%3CLI%3E%23%20Name%20of%20the%20mailboxes%20accepts%20all%20the%20types%20of%20mailbox%20names%20that%20can%20be%20specified%20to%20get-mailbox%20cmdlet%3C%2FLI%3E%0A%3CLI%3E%23%20ex%3A%20SearchMailboxes.ps1%20user1%3C%2FLI%3E%0A%3CLI%3E%23%20ex%3A%20SearchMailboxes.ps1%20user*%3C%2FLI%3E%0A%3CLI%3E%23%20ex%3A%20SearchMailboxes.ps1%20*user*%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22filename%20bold%22%3ESearch.Config%3C%2FSPAN%3E%3A%20Open%20Search.Config%20and%20edit%20the%20value%20of%20Discovery%20mailbox.%20This%20command%20gets%20the%20Discovery%20mailbox%3A%3CP%20class%3D%22code%22%3EGet-Mailbox%20-Identity%20Discovery*%3C%2FP%3E%0A%3CP%3EEdit%20the%20start%20date%20to%20go%20back%20number%20of%20days%20to%20get%20the%20first%20search%20results.%20%3CBR%20%2F%3EEdit%20the%20keywords%20and%20separate%20them%20with%20the%20conjunction%20'OR'%20%3CBR%20%2F%3EThis%20is%20a%20mandatory%20file%20and%20it%20needs%20to%20be%20present%20in%20the%20same%20location%20as%20%3CSPAN%20class%3D%22filename%22%3ESearchMailboxes.ps1%3C%2FSPAN%3E.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22filename%20bold%22%3ELastSearch.txt%3C%2FSPAN%3E%3A%20This%20file%20is%20generated%20when%20the%20SearchMailboxes.ps1%20script%20is%20run%20for%20the%20first%20time.%20It%20stores%20the%20information%20about%20the%20last%20time%20the%20search%20was%20run.%20If%20this%20file%20isn't%20present%2C%20the%20SearchMailboxes.ps1%20script%20will%20pick%20the%20start%20date%20of%20the%20search%20from%20the%20Search.config%20file.%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22filename%20bold%22%3ESearchMailboxes.ps1%3C%2FSPAN%3E%3A%20This%20is%20the%20script%20that%20consumes%20%3CSPAN%20class%3D%22filename%22%3Esearch.config%3C%2FSPAN%3E%20and%20%3CSPAN%20class%3D%22filename%22%3ElastSearch.txt%3C%2FSPAN%3E%20to%20generate%20the%20search%20results.%20It's%20not%20required%20to%20modify%20this%20script%20because%20all%20configurable%20parameters%20are%20contained%20in%20the%20other%20files%20in%20this%20list.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CSPAN%20class%3D%22bold%22%3EDownload%20the%20script%3C%2FSPAN%3E%3A%20You%20can%20download%20this%20script%20and%20all%20of%20the%20associated%20files%20%3CA%20target%3D%22_blank%22%20href%3D%22http%3A%2F%2Fgallery.technet.microsoft.com%2FExchange-Server-2010-9f7c1e31%22%20title%3D%22Exchange%20Server%202010%20Discovery%20Search%20(Multi-Mailbox%20Search)%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22bold%22%3EEDIT%2011%2F30%2F2010%3A%3C%2FSPAN%3E%20Added%20a%20note%20that%20the%20multi-mailbox%20search%20is%20an%20Enterprise%20CAL%20feature.%3CSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22author%22%3E%3CA%20href%3D%22http%3A%2F%2Fblogs.technet.com%2Farticles%2F456957.aspx%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EKunal%20Nikam%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Apr 29 2020 11:43 AM
Updated by: