Quantcast
Channel: Exchange Server 2013 - General Discussion forum
Viewing all articles
Browse latest Browse all 4521

Need Help With Script to Export mailbox to PST (New-MailboxExportRequest) And delete the exported mails ( Search-Mailbox -SearchQuery -DeleteContent)

$
0
0

Hello I need some help with my script.

What i would like to do is to export once a month the content of a mailbox and delete the content after the export process is complete.

$SourceMailbox = "Mailbox"
$endDate = Get-Date -Day 1 "00:00:00"
$startDate = $endDate.AddDays(-1)
$enddatestring = $enddate.ToString('MM/dd/yyyy')
$startdatestring = $startdate.ToString('MM/dd/yyyy')
$dateTime = Get-Date -format "MMddyyyy_hhmmsstt"
$archiveDate = (Get-Date).ToString('dd_MM_yyyy')
$archiveJobName = $SourceMailbox + " " + $archiveDate + $dateTime
$ExportContentFilter = "(Received -ge '$startDatestring') -and (Received -lt '$endDatestring')"
$archiveFile = "archive " + $archiveDate + ".pst"
$archiveFileDir = "\\192.168.1.111\PSTPathFolder\"
$archiveFilePath = $archiveFileDir + $archivefile
$BatchName = "Mailbox " + $archiveDate + $endDate
$searchStartDate = $startDate.ToString('MM/dd/yyyy')
$searchEndDate = (Get-Date).AddDays(- (Get-Date).Day).ToString('MM/dd/yyyy')
$searchDateRange = "Received:" + $searchStartDate + ".." + $searchEndDate
$saveSearchLogMailbox = "LOG MAILBOX"
$saveSearchLogFolder = "LOG MAILBOX SUB FOLDER" 
New-MailboxExportRequest -Mailbox $SourceMailbox -Name $archiveFile -ContentFilter $ExportContentFilter -FilePath "$archiveFilePath" -IncludeFolders "inbox"


# Wait for the archiving job to complete

while (!(Get-MailboxExportRequest -Name $archiveJobName -Status Completed))
{
	Start-Sleep -Seconds 1800
}


do
{
	# Creating a log file of email messages to be deleted
	$remainingItemCount = Get-Mailbox -Identity $SourceMailbox | Search-Mailbox -SearchQuery $searchDateRange -LogOnly -LogLevel Full -TargetFolder $saveSearchLogFolder -TargetMailbox $saveSearchLogMailbox | select -expand ResultItemsCount
	# Delete email
	Get-Mailbox -Identity $SourceMailbox | Search-Mailbox -SearchQuery $searchDateRange -DeleteContent -force
	# Sleep for 5 minutes only when there are more email messages to delete (may not be required)
	if ($remainingItemCount -eq 10000)
	{
		start-sleep -s 300
	}
}
while ($remainingItemCount -eq 10000)


I Will run the script with task scheduler once a month

I Have Exchange 2010 sp3






Viewing all articles
Browse latest Browse all 4521

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>