Format between object types

Welcome Forums General PowerShell Q&A Format between object types

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #166331

    Participant
    Topics: 2
    Replies: 2
    Points: 31
    Rank: Member

    Hi everybody 🙂

    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

     

  • #166355

    Participant
    Topics: 2
    Replies: 2
    Points: 31
    Rank: Member

    I have managed to do it like this.

    Silly me.

     

    $Timestamp = $Trace.Received.toString()
  • #166367

    Participant
    Topics: 2
    Replies: 2
    Points: 31
    Rank: Member

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

  • #166399

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    Is there a way to escape all single and double quotes in the $subject?

    Yes

    What would a regex look like?

    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".')

You must be logged in to reply to this topic.