Format between object types

Hi everybody :slight_smile:

I’m having a nightmare converting between two object types.

My script is designed to get output from the Get-MessageTrace cmdlet.

Running the command and piping it to Get-Member I can see that the Received property has a DateTime type.

The Search-Mailbox -SearchQuery is using only strings.

This is what I made so far, but when I run it it gives me an error.

function Search-ABMEmail {
#clear variable values
$Trace = $null
$ID = $null
$Trace = $null
$RecipientAddress = $null
$SenderAddress = $null
$Subject = $null


#get trace
$ID = Read-Host -Prompt "Enter MessageID "
$Trace = Get-MessageTrace -MessageID "$ID" | Where-Object status -eq "Delivered"
$RecipientAddress = $Trace.RecipientAddress
$SenderAddress = $Trace.SenderAddress
$Subject = $Trace.Subject
$Timestamp = {$Trace.Received}.toString("MM/dd/yyyy hh:mm:ss tt")

$Trace

#foreach ($Identity in $Trace) {
Get-Mailbox -Identity $RecipientAddress | Search-Mailbox -SearchQuery "Subject:$Subject AND From:$SenderAddress AND Received:$Timestamp" -EstimateResult
#Get-Mailbox -Identity $RecipientAddress | Search-Mailbox -SearchQuery "Subject:$Subject AND From:$SenderAddress" -DeleteContent
#}
}
#initiate
Search-ABMEmail



Cannot find an overload for "toString" and the argument count: "1".
At H:\Personal\search.ps1:17 char:1
+ $Timestamp = {$Trace.Received}.toString("MM/dd/yyyy hh:mm:ss tt")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

 

I have managed to do it like this.

Silly me.

 

$Timestamp = $Trace.Received.toString()

Is there a way to escape all single and double quotes in the $subject? What would a regex look like? :slight_smile:

Yes

You don’t have to think about that in that case … let Powershell ( or dotNet ) do the Job for you:

[regex]::Escape(‘Some arbitrary content with “Quotes”.’)