Link to home
Start Free TrialLog in
Avatar of Member_2_6492660_1
Member_2_6492660_1Flag for United States of America

asked on

Exchange 2010 New-MailboxExportRequest Script Help

Exchange Server 2010 SP1 Enterprise 64 bit
Windows 2008 R2 64 bit

Every week I export the journal mailbox to a pst file.

I had this for Exchange 2007 journal.ps1      which is called from a cmd file journal.cmd

 @echo off
del c:\util\journal.txt
cd\util
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command "./journal.ps1"
Exit

The 2007 journal.ps1

$today = Get-Date -Format "MMddyyyy"
$Folder = "O:\email\journal\journal-$today"
Export-Mailbox –Identity journal -PSTFolderPath "$folder.pst" -confirm:$false -verbose -baditemlimit 99999 -StartDate $((Get-date).AddDays(-7)) –EndDate $(Get-date) -deletecontent >>journal.txt


Now on 2010 you have New-MailboxExportRequest

$today = Get-Date -Format "MMddyyyy"
$Folder = "O:\email\journal\journal-$today"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -confirm:$false -verbose -baditemlimit 99999 -StartDate $((Get-date).AddDays(-7)) –EndDate $(Get-date) -deletecontent >>journal.txt


The above fails on -Startdate

Trying to export the inbox by the previous week  this runs every Monday at 5:00 PM

The output file journal-01052015.pst is the file format I would like to preserve.


Thanks
Avatar of Brendan M
Brendan M
Flag of Australia image

there is no StartDate parameter in the New-MailboxExportRequest https://technet.microsoft.com/en-us/library/ff601762(v=exchg.141).aspx

though you can user the ContentFilter parameter (Send and Received)

https://technet.microsoft.com/en-us/library/ff601762(v=exchg.141).aspx
Avatar of Member_2_6492660_1

ASKER

Brendan

I tried that thanks see my results

$today = Get-Date -Format "MMddyyyy"
$Folder = "O:\email\journal\journal-$today"
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

$start = (get-date).adddays(-7)
$end = (get-date).adddays
$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -ContentFilter $filter -confirm:$false -verbose -baditemlimit 1 >>journal.txt



Output

C:\Util>journal

C:\Util>rem @echo off

C:\Util>del c:\util\journal.txt

C:\Util>cd\util

C:\Util>C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -PSConsoleFile
 "D:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command "./j
ournal.ps1"
VERBOSE: [00:33:02.536 GMT] New-MailboxExportRequest : Initializing Active
Directory server settings for the local Windows PowerShell session.
VERBOSE: [00:33:03.170 GMT] New-MailboxExportRequest : Active Directory session
 settings for 'New-MailboxExportRequest' are: View Entire Forest: 'True',
VERBOSE: [00:33:03.708 GMT] New-MailboxExportRequest : Runspace context:
Executing user: our.network.tgcsnet.com/Users/Administrator, Executing user
organization: , Current organization: , RBAC-enabled: Disabled.
VERBOSE: [00:33:03.715 GMT] New-MailboxExportRequest : Beginning processing
New-MailboxExportRequest
VERBOSE: [00:33:04.074 GMT] New-MailboxExportRequest : Instantiating handler
with index 0 for cmdlet extension agent "Admin Audit Log Agent".
WARNING: When an item can't be read from the source location or can't be
written to the target location, it will be considered corrupted. By specifying
a non-zero BadItemLimit, you're requesting that Exchange not copy such items to
 the target location. When the request is complete, such corrupted items won't
be available at the target. When the source is deleted, these items will be
completely lost.
VERBOSE: [00:33:04.507 GMT] New-MailboxExportRequest : Admin Audit Log: Entered
 Handler:OnComplete.
The provided ContentFilter value is invalid. ContentFilter is invalid. The valu
e "System.DateTime AddDays(double value)" could not be converted to type System
.DateTime. --> The value "System.DateTime AddDays(double value)" could not be c
onverted to type System.DateTime.
At C:\Util\journal.ps1:10 char:1
+  <<<< New-MailboxExportRequest -Mailbox journal -FilePath "$folder.pst" -Cont
entFilter $filter -confirm:$false -verbose -baditemlimit 1 >>journal.txt
    + CategoryInfo          : InvalidArgument: ((Received -gt '...double value
   )'):String) [], ContentFilterInvalidPermanentException
    + FullyQualifiedErrorId : 1FD9192E


C:\Util>Pause
Press any key to continue . . .
Terminate batch job (Y/N)? y

C:\Util>


Thoughts
$today = Get-Date -Format "MMddyyyy"
$Folder = "O:\email\journal\journal-$today"
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

$start = (get-date).adddays(-7)
$end = (get-date)
$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -ContentFilter "$filter" -confirm:$false -verbose -baditemlimit 1 >>journal.txt

Open in new window


please try
Branden

Great

I had to also modify the $folder value was invalid as it was had to use this instead \\10.2.8.20\volume_1\email\journal\journal-$today

In 2007 the export had this parameter " -deletecontent "  this is not an 2010 option

It cleared the inbox after the export ran.


How can I do that now?

would search-mailbox work?

I just want to cleared the inbox of all records after the export

Thoughts
Branden

Search-Mailbox -Identity "journal" -SearchQuery  ?????????????  -DeleteContent


Where can I find the options for -Searchquery?

If I want to delete the entire inbox?
example:
Search-Mailbox -Identity usermailbox -SearchQuery {sent:02/01/2013..02/30/2013} -LogOnly -LogLevel full

it uses Advanced Query Syntax
https://msdn.microsoft.com/en-us/library/aa965711%28v=vs.85%29.aspx


to delete everything
just leave the -searchquery blank
Brandan

Thanks

Search-Mailbox -Identity usermailbox -SearchQuery {sent:'$start'..'$end'} -LogOnly -LogLevel full


will that work?
maybe, you might have to use

$start = (get-date).adddays(-7).Date
$end = (get-date).Date

Open in new window

Brandan

Sounds good will test that out

One other thing

If I add that to my journal.ps1 script as the last line will it try to delete the items before the nw-mailboxexportrequest completes?



So this is the complete script

$today = Get-Date -Format "MMddyyyy"
$Folder = "\\10.2.8.20\volume_1\email\journal\journal-$today"
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

$start = (get-date).adddays(-7) .Date
$end = (get-date) .Date
$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -ContentFilter "$filter" -confirm:$false -verbose -baditemlimit 1 >>journal.txt


Search-Mailbox -Identity usermailbox -SearchQuery {sent:'$Start'..'$End'} -LogOnly -LogLevel full -DeleteContent
Branden

Testing this

example:
 Search-Mailbox -Identity usermailbox -SearchQuery {sent:02/01/2013..02/30/2013} -LogOnly -LogLevel full

I get Specify Folder prompt what am I missing?
Looks like you need to be using something similar to the following:

$today = Get-Date -Format "MMddyyyy"
$Folder = "\\10.2.8.20\volume_1\email\journal\journal-$today"
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

$start = (get-date).adddays(-7) .Date
$end = (get-date) .Date
$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -ContentFilter {(Received -lt $end) -and (Received -gt $start)} -confirm:$false -verbose -baditemlimit 1 >>journal.txt

Don't quote me - I'm no scripting guru!!

Alan
Alan

Thanks neither am I just dabble a little

I have that part working on exchange 2007 you had export-mailbox that had a -deletecontent

Which was removed in 2010

Now you need

Search-Mailbox -Identity usermailbox -SearchQuery {sent:02/01/2013..02/30/2013} -LogOnly -LogLevel full -deletecontent


But my test was not to delete just running this

Search-Mailbox -Identity usermailbox -SearchQuery {sent:02/01/2013..02/30/2013} -LogOnly -LogLevel full

That is when it prompts me for a folder name
Extract from (https://technet.microsoft.com/en-us/library/dd298173.aspx):

"Use the Search-Mailbox cmdlet to search a mailbox and copy the results to a specified target mailbox"

Therefore you will need the -TargetMailbox "Mailboxname" switch to search and copy the search results to a mailbox.
Alan

I saw that tech note

I was trying to use Example three (3)

Search-Mailbox -Identity "April Stewart" -SearchQuery 'Subject:"Your bank statement"' -DeleteContent

Just trying to  change the -Searchquery around
Do you have the relevant permissions setup on the account you are using to run the command as per the following link:

Relevant Section:
"Delete mailbox content (using the Search-Mailbox cmdlet with the DeleteContent switch)"

https://technet.microsoft.com/en-us/library/dd638205.aspx
Alan

I ran this the other day

PS] C:\Windows\system32>new-managementroleassignment -role "mailbox import export" -user administrator

Name                           Role              RoleAssigneeName  RoleAssigneeType  AssignmentMethod  EffectiveUserNam
                                                                                                       e
----                           ----              ----------------  ----------------  ----------------  ----------------
Mailbox Import Export-Admin... Mailbox Import... Administrator     User              Direct

I am running this script from the administrator account on the exchange server  (domain admin)
What about the discovery management role too?
Alan add it the same way?

new-managementroleassignment -role "Discovery Management" -user administrator
Alan

I think I got it

[PS] C:\Windows\system32>get-rolegroupmember "Discovery Management"
[PS] C:\Windows\system32>add-rolegroupmember "Discovery Management" -Member Administrator
[PS] C:\Windows\system32>get-rolegroupmember "Discovery Management"

Name                                                        RecipientType
----                                                        -------------
Administrator                                               UserMailbox


[PS] C:\Windows\system32>

Going to test the search-mailbox cmdlet now
Guys

I have the command almost working


This is it

PS] C:\Windows\system32>search-mailbox -identity 'exchange journal' -searchquery "Received:> $('09/29/2014') and Received:< $('09/30/2014')"  -deletecontent >c:\temp\delete09.txt

Confirm
Deleting content from mailboxes exchange journal
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): a
Search Failed on mailbox 'our\Exchange Journal\16b6e8c3-75ed-4fb2-8380-4d3e86e84016'. If the search query used short words with wildcard(s) such as pat*, it can result in a large number of words or phrases because all permutations of word
s starting with "pat" are searched. Make search queries as specific as possible when using wildcards.
    + CategoryInfo          : ReadError: (0:Int32) [Search-Mailbox], SearchMailboxException
    + FullyQualifiedErrorId : 7113682B,Microsoft.Exchange.Management.Tasks.SearchMailbox


ran this

test-exchangesearch journal | fl

it gave me many results so it found it.


Any thoughts?

Thanks
is the service "Microsoft Search (Exchange)" and "Microsoft Exchange Search Indexer" running in services?
Hi Brandan

Yes both services are running

Microsoft Search (Exchange) is set to manual
Microsoft Exchange Search Indexer is set to automatic

Again both are started


I was looking at this  before

https://social.technet.microsoft.com/Forums/exchange/en-US/3ca4f5c6-ca45-454d-9edf-788654a1c718/exchange-2010-sp1-searchmailbox-deletecontent-switch-seems-to-kill-the-query-if-the-logonly


Thoughts
This any better?

search-mailbox -identity 'exchange journal' -searchquery "Received:09/29/2014..09/30/2014"  -deletecontent >c:\temp\delete09.txt
All

That worked great.

Results

search-mailbox -identity 'exchange journal' -searchquery "Received:09/29/2014..09/30/2014"  -deletecontent >c:\temp\delete09.txt


[PS] C:\Windows\system32>search-mailbox -identity 'exchange journal' -searchquery "Received:01/12/2015..01/17/2015"  -de
letecontent >c:\temp\delete17.txt

Confirm
Deleting content from mailboxes exchange journal
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): y
[PS] C:\Windows\system32>


How do I auto respond to this prompt
Try this:

search-mailbox -identity 'exchange journal' -searchquery "Received:01/12/2015..01/17/2015"  -deletecontent >c:\temp\delete17.txt -confirm:$false
Alan

Sorry tried that still prompting me

Thoughts
ASKER CERTIFIED SOLUTION
Avatar of Alan Hardisty
Alan Hardisty
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alan

That did it all my research could not find -force

Now I need to build the script so I can automate this
Gleamed that little gem from here:

https://technet.microsoft.com/en-us/library/dd298173(v=exchg.141).aspx

Parameters section.
All

Here is what I did so far

today = Get-Date -Format "MMddyyyy"
$Folder = "\\10.2.8.20\volume_1\email\journal\journal-$today"
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

add-pssnapin Microsoft.*

$start = (get-date).adddays(-7)
$end = (get-date)
$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
New-MailboxExportRequest –Mailbox journal -FilePath "$folder.pst" -ContentFilter "$filter" -confirm:$false -verbose -baditemlimit 1 >>c:\util\journal.txt

Get-MailboxExportRequest | where {$_.status -eq "Completed"} >>c:\util\journal.txt

search-mailbox -identity 'exchange journal' -searchquery "Received:'"+$start+"'..'"+$end+"'"  -deletecontent >>c:\util\journal.txt -force


My concern is about the -searchquery "Received:'"+$start+"'..'"+$end+"'"


How does it look
Alan

Thanks for your help

I am opening another question about this

I post it on here when I create it