Exchange Power Shell Script not working.

The script runs no problem only thing it does not clear the inbox data

 

These two lines of the code

search-mailbox -identity journalman -searchquery “$FilterS” -deletecontent -force | out-file $LogFolder$LogFile -append
search-mailbox -identity spamman -searchquery “$FilterS” -deletecontent -force | out-file $LogFolder$LogFile -append

 

The full script

$Server = hostname
$Subject = “$Server Export Journal\Spam”
$Body = “Open attachment for Export Journalman/Spamman Inboxes Weekly Report”
$From = “no-reply@mynet.com
$To = “systems-alert@mynet.com
$LogFolder = “c:\util\logs”
$LogFile = “journalman.txt”
$MRSLog1 = “MRS-History-Spamman.Log”
$MRSLog2 = “MRS-History-Journalman.Log”
$today = Get-Date -Format “MMddyyyy”
$Folder = “\10.12.18.23\PST\journalman-$today.pst”
$FolderS = “\10.12.18.23\PST\spamman-$today.pst”
$FolderM = “\10.12.18.13\usbshare1\PST”
$FolderX = “\10.12.18.23\PST”

$PSEmailServer = “InternalRelay.MYNET.COM

remove-item -path $LogFolder$LogFile -Force
remove-item -path $LogFolder$MRSLog1 -Force
remove-item -path $LogFolder$MRSLog2 -Force

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture(“en-us”)

Add-PSSnapin microsoft.exchange.management.powershell.SnapIn;

$start = (get-date).adddays(-7).toString(‘MM/dd/yyyy’)
$end = (get-date).toString(‘MM/dd/yyyy’)
$filter = “(Received>=”+$start+") and (Received<="+$end+")"
$filterE = “(Received -gt '”+$start+"’) -and (Received -lt ‘"+$end+"’)"
$filterS = “received<=”+$end+" and received>="+$start+""

Get-MailboxExportRequest | out-file $LogFolder$LogFile -append

Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest -confirm:$false | out-file $LogFolder$LogFile -append

New-MailboxExportRequest –Mailbox Journalman -Name MyNextAttempt -FilePath $folder -ContentFilter “$filterE” -confirm:$false -verbose -baditemlimit 100 -AcceptLargeDataLoss | out-file $LogFolder$LogFile -append

New-MailboxExportRequest –Mailbox spamman -Name MyNextAttempt -FilePath $folderS -ContentFilter “$filterE” -confirm:$false -verbose -baditemlimit 100 -AcceptLargeDataLoss | out-file $LogFolder$LogFile -append

start-sleep -Seconds 300

Write-Output “Clearing Mailboxes Content Now” | out-file $LogFolder$LogFile -append

search-mailbox -identity journalman -searchquery “$FilterS” -deletecontent -force | out-file $LogFolder$LogFile -append
search-mailbox -identity spamman -searchquery “$FilterS” -deletecontent -force | out-file $LogFolder$LogFile -append

Get-MailboxExportRequest | Where {$.Status -eq “Completed”} | out-file $LogFolder$LogFile -append
Get-MailboxExportRequest | Where {$
.Status -eq “Completed”} | Remove-MailboxExportRequest -confirm:$false | out-file $LogFolder$LogFile -append

Test-MRSHealth | out-file $logfolder$logfile -Append

$MoveHistory = Get-MailboxStatistics “spamman”
$MoveHistory[0] | Out-file –FilePath $logfolder$MRSlog1
$MoveHistory = (Get-MailboxStatistics “journalman” -IncludeMoveReport).movehistory
$MoveHistory[0] | Out-file –FilePath $logfolder$MRSlog2

move-item -path $folderx*.pst -destination $folderM | out-file $LogFolder$LogFile -append
get-childitem -Path $folderm -Recurse | out-file $LogFolder$LogFile -append

send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -smtpserver $PSEmailServer -attachment “$LogFolder$LogFile”, “$logfolder$MRSlog1”, “$logfolder$MRSlog2”

 

 

Any ideas?

 

Thank you,

Tom

 

As for…

The script runs no problem only thing it does not clear the inbox data

…, does this mean, you looked at the inbox, saw that their was mail there, then ran this code, and looked at it again, and the mail was still there?
Or does this mean, you ran the code, and then did a recover?
If it is the later, this will happen if there is a litigation hold policy on the user mailbox.

Otherwise, how to you know you are actually getting any results from the search?

What happens if you just do the below in your code?

(search-mailbox -identity journalman -EstimateResultOnly).ResultItemsCount
(search-mailbox -identity journalman -searchquery "$FilterS" -EstimateResultOnly).ResultItemsCount

PS C:\util> (search-mailbox -identity journalman -EstimateResultOnly).ResultItemsCount
WARNING: The Search-Mailbox cmdlet returns up to 10000 results per mailbox if a search query is specified. To return
more than 10000 results, use the New-MailboxSearch cmdlet or the In-Place eDiscovery & Hold console in the Exchange
Administration Center.
48042
PS C:\util> (search-mailbox -identity journalman -searchquery “$FilterS” -EstimateResultOnly).ResultItemsCount
WARNING: The Search-Mailbox cmdlet returns up to 10000 results per mailbox if a search query is specified. To return
more than 10000 results, use the New-MailboxSearch cmdlet or the In-Place eDiscovery & Hold console in the Exchange
Administration Center.
No results were returned for this search query.

  • CategoryInfo : InvalidArgument: (:slight_smile: [], ArgumentException
  • FullyQualifiedErrorId : [Server=TGCS021-N1,RequestId=6e0af823-696e-4dda-80be-3249a414c2f3,TimeStamp=2/16/2019 2:
    31:39 PM] [FailureCategory=Cmdlet-ArgumentException] 115777F2
  • PSComputerName : tgcs021-n1

That warning message is expected, and can be safely ignored. However, this…

No results were returned for this search query.

… says the filter / search query you are passing in is not correct.

This is why, this ….
(search-mailbox -identity journalman -EstimateResultOnly).ResultItemsCount
… shows success, by returning the proper count of 48042, and the other fails with your filter / search string fails.

That search query must be a string match explicitly. For example:

Search-Mailbox SomeUserName -SearchQuery 'Subject:"test"'

Since you do not show your string, or a sample subject for comparison. that just leaves us to guess.