Solved

Exchange 2010 New-MailboxExportRequest Script Help

Posted on 2015-01-26
32
408 Views
Last Modified: 2015-02-02
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
0
Comment
Question by:Thomas Grassi
  • 18
  • 8
  • 6
32 Comments
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40571758
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40571771
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
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40571787
$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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40571836
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
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40571897
have a look at the "removing messages" section

http://blogs.technet.com/b/exchange/archive/2010/10/27/3411398.aspx
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40571937
Branden

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


Where can I find the options for -Searchquery?

If I want to delete the entire inbox?
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40571991
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40572059
Brandan

Thanks

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


will that work?
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40572069
maybe, you might have to use

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

Open in new window

0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40572080
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40573226
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?
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40573371
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40573384
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
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40573413
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.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40573427
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
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40573434
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40573479
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)
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40573818
What about the discovery management role too?
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40573889
Alan add it the same way?

new-managementroleassignment -role "Discovery Management" -user administrator
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40574018
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40574308
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
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 40574323
is the service "Microsoft Search (Exchange)" and "Microsoft Exchange Search Indexer" running in services?
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40574328
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
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40574724
This any better?

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

Author Comment

by:Thomas Grassi
ID: 40576398
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
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40576500
Try this:

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

Author Comment

by:Thomas Grassi
ID: 40576571
Alan

Sorry tried that still prompting me

Thoughts
0
 
LVL 76

Accepted Solution

by:
Alan Hardisty earned 500 total points
ID: 40576642
Okay - then please try this:

search-mailbox -identity 'exchange journal' -searchquery "Received:01/12/2015..01/17/2015"  -deletecontent >c:\temp\delete17.txt -force
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40576644
Alan

That did it all my research could not find -force

Now I need to build the script so I can automate this
0
 
LVL 76

Expert Comment

by:Alan Hardisty
ID: 40576647
Gleamed that little gem from here:

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

Parameters section.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 40576650
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
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
ID: 40585120
Alan

Thanks for your help

I am opening another question about this

I post it on here when I create it
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now