Search-mailbox querystring help

Highlighted
Occasional Visitor

Powershell Help

I have a script that will read information from a CSV file and will delete emails in O365 using the search-mailbox with the delete content switch.
I am getting different results and I think it is a problem with my query string. The email to delete is found using the email address, subject and the date and time sent. This should result in only one email found. What is happening is when I test the script the logs say more emails are found. I have output the query string using write-host then applied it manually to the search mailbox command and only get one result. I am struggling to figure out why I get different results.


#18-6-18
#Version 1.0 Initial Script
write-host "`n"
write-host "Welcome to the GDPR Erasure Tool Version 1.2 `n"

$ConnectToO365 = read-host "Press C to connect to O365 `n"

if ($ConnectToO365 -eq "c") {
#Connect to O365
$Cred = Get-Credential -Credential
$sessionOption = New-PSSessionOption -SkipRevocationCheck
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Cred -Authentication Basic -AllowRedirection -SessionOption $sessionOption
Import-PSSession $session
Connect-MsolService -Credential $cred
}
elseif ($ConnectToO365 -ne "c") {
write-host "connecting to O365 Skipped... `n`n"
}

#Function to pop up the open file dialog box in Windows
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}

function ConToUSDate ($date) {

##This function converts the date and time into US and format it so that the search-Mailbox can use it.
$dateTime = $date -split " "
$USDSplit = $dateTime -split '/'
$usd= $USDSplit[1]+'/'+$USDSplit[0]+'/'+$USDSplit[2]
$Output = $usd+'..'+$datetime[1]

return $output
}
#ConToUSDate -date '15/06/2018 11:30'

Function escape-query ($queryString){

$queryString -replace ":", "``:"
return
}

write-host "You will require a results.csv file from a content search to select the emails to be deleted `n"
write-host "Please select the results.csv `n`n "

pause

$inputfile = Get-FileName
$savePath = $inputfile.Substring(0, $inputfile.lastIndexOf('\')) + "\"

write-host "To Test Select 1`nTo Examine the query strings select 2 `nTo Run the Delete Process select 3`n "

$test = read-host "Option"

if ($test -eq "1") {
write-host "Running Script in test mode...`n"
import-csv $inputfile | ForEach-Object {search-mailbox $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" -EstimateResultOnly } | Export-Csv -NoTypeInformation -Append -Path "$savePath TestLog.csv"
}
elseif ($test -eq "3"){
write-host "Running Live Delete...Press ctl C to Cancel`n"
pause
import-csv $inputfile | ForEach-Object {search-mailbox $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" -DeleteContent -confirm:false } | Export-Csv -NoTypeInformation -Append -Path "$savePath DeleteLog.csv"
}
elseif($test -eq "2"){
#Show the search string for troubleshooting......
import-csv $inputfile | ForEach-Object {write-host $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" }
}

$CloseConnection = Read-Host "Close the O365 Connection? y/n"

if ($CloseConnection -eq "y") {
Write-Host "O365 connection closed..."
Get-PSSession | Remove-PSSession
}
else{
Write-Host "O365 connection still open..."
}

pause


Spreadsheet example. It is from the results.csv

Location Name,Subject or Title, Sent
user.name@domain.com, Test Compliance Search, 15/06/2018 11:27:18

 

0 Replies