Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when executing Get-Email #111

Open
Ian-988 opened this issue Dec 3, 2024 · 5 comments
Open

Error when executing Get-Email #111

Ian-988 opened this issue Dec 3, 2024 · 5 comments

Comments

@Ian-988
Copy link

Ian-988 commented Dec 3, 2024

I am trying to download all Emails based on their Message ID using your very useful tool (many thanks)

I have run the following command:

"Get-Email" -userIds sr@ad.com -internetMessageId "<CWL7E2@CWL***.OUTLOOK.COM>"

I get the following error:
[ERROR] An error occurred: Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime."

If I run the "Show-Email" command with the same parameters the details are returned successfully.

This happens for many Emails (and some are successful for the same user / mailbox)

I can download different emails successfully using the same session.

Any ideas?
Thanks
Ian

@JoeyInvictus
Copy link
Collaborator

Hi, we're glad you find our tool helpful! We are aware of the issue around the String was not recognized as a valid DateTime. error. This is mainly caused by attempting to retrieve an email that is no longer in the user's mailbox. As a result, no data is returned, and our script tries to parse the data to retrieve the receivedDateTime field.

In the next update, we will add an extra check to skip the email if it's not found in the mailbox, and we will prevent the script from attempting to parse it. And as a fallback, if the email is present but cannot be parsed, it will simply remove the receivedDateTime from the filename.

However, it's strange that you can view the emails with Show-Email. I did some testing, and when the email is removed, Show-Email shouldn't work either. Can you try the following steps manually to identify where the script is failing?

Retrieve the email and save the data returned to $getMessage:
$getMessage = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$userIds/messages?filter=internetMessageId eq '$id'"

Make sure to replace $userIds with the relevant username and $id with the InternetMessageId of the email.

Next, save the relevant part of the output to the $message variable:
$message = $getMessage.value[0]

Now, if you run $message, you should see the output of the email, confirming that it was received successfully:
$message

image

Additionally, we can check the problematic field and inspect its value:
$message.receivedDateTime

What is returned when you run this?

Finally, you can try to parse this value by running:
[datetime]::Parse($message.receivedDateTime).ToString("yyyyMMdd_HHmmss")

image

@Ian-988
Copy link
Author

Ian-988 commented Dec 4, 2024

Hi there.

Thanks for the prompt response!
I tried the commands requested.

$getMessage = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$userIds/messages?filter=internetMessageId eq '$id'"

This returned the email content for the same email that I could not download using "Get-Email":
Screenshot 2024-12-04 173003

$message.receivedDateTime returned the following:
25 November 2024 10:25:00

[datetime]::Parse($message.receivedDateTime).ToString("yyyyMMdd_HHmmss") returned the following:
Exception calling "Parse" with "1" argument(s): "String was not recognized as
a valid DateTime."
At line:1 char:1

Thanks
Ian

@JoeyInvictus
Copy link
Collaborator

Hi,

Interesting that you're encountering this error, when I manually parse the data, it works without any issues. I'm not sure how to fix this right now and will need to do more testing to see if I can reproduce the problem.

image

As a temporary workaround, you could try modifying the Get-Emails.ps1 script in the Scripts directory. Replace lines 123 through 133:

 $ReceivedDateTime = [datetime]::Parse($message.receivedDateTime).ToString("yyyyMMdd_HHmmss")
$messageId = $message.id
 $subject = $message.Subject -replace '[\\/:*?"<>|]', '_'

 if ($output -eq "txt") {
    $filePath = "$outputDir\$ReceivedDateTime-$subject.txt"
 }
            
else {
     $filePath = "$outputDir\$ReceivedDateTime-$subject.eml"
 }

With this updated block:

try {
    $ReceivedDateTime = [datetime]::Parse($message.receivedDateTime).ToString("yyyyMMdd_HHmmss")
    $datePrefix = "$ReceivedDateTime-"
}
catch {
    Write-LogFile -Message "[WARNING] Could not parse received date, skipping date prefix in filename" -Color "Yellow" -Level Standard
    $datePrefix = ""
}

$messageId = $message.id
$subject = $message.Subject -replace '[\\/:*?"<>|]', '_'

if ($output -eq "txt") {
    $filePath = "$outputDir\$datePrefix$subject.txt"
}
else {
    $filePath = "$outputDir\$datePrefix$subject.eml"
}

This change uses a try block to parse the date. If successful, it creates a date prefix for the filename. If it fails, it sets an empty prefix, ensuring the filename is still valid with just the subject. This way, even if the date parsing fails, you'll still get the full email without errors.

In our next update, we'll add more try/catch blocks to help prevent these errors from occurring. I'll also investigate further to see if I can reproduce the issue on my end. Let me know if the temporary solution works for you!

@Ian-988
Copy link
Author

Ian-988 commented Dec 5, 2024

Hi there.

The temporary workaround worked for me, many thanks.
I did notice that it was now overwriting emails with the same subject line, but this is good enough for now.

Many thanks for your help and prompt responses, much appreciated!
Ian

@JoeyInvictus
Copy link
Collaborator

Hi @Ian-988 ,

Yeah, this is something we've been dealing with for a while. Initially, we made the messageId the filename, but since multiple users can receive the same message, this caused duplicate filenames. To address this, we started using the subject to make it more readable. However, this also led to duplicates, so we decided to add the received date to the filename as well. Even with this approach, we still encounter duplicates from time to time...

In our next update, we'll be adding a prefix to each email we collect, so you'll see filenames like 001, 002, 003-date-subject, and so on. This should eliminate the issue of duplicates once and for all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants