Jump to content

[PSGSuite] Dump all emails between users with date range


Recommended Posts

Posted

[string]$Sender = "[color="#FF0000"]User1[/color]"
[string]$Recipient = "[color="#FF0000"]User2[/color]"
[string]$StartDate = "[color="#FF0000"]XXXX/XX/XX[/color]" # Datestamp in YYYY/MM/DD
[string]$EndDate = "[color="#FF0000"]XXXX/XX/XX[/color]" # Datestamp in YYYY/MM/DD
[string]$SaveRoot = "[color="#FF0000"]C:\Path[/color]"

If (!(Test-Path ($SaveRoot + "\" + $Sender + " to " + "$Recipient"))) {
   New-Item -Path ($SaveRoot + "\" + $Sender + " to " + "$Recipient") -ItemType Directory
}
ForEach ($EmailList in (Get-GSGmailMessageList -User $Recipient -Filter ("from:" + $Sender), ("after:" + $StartDate), ("before:" + $EndDate) -ExcludeChats)) {
   ForEach ($Email in (Get-GSGmailMessage -User $Recipient -Id $EmailList.Id -ParseMessage)) {
       [string]$FullFilePath = ($SaveRoot + "\" + $Sender + " to " + "$Recipient" + "\" + ([string]$Email.Date.Year + '-' + ([string]$Email.Date.Month).PadLeft(2,'0') + '-' + ([string]$Email.Date.Day).PadLeft(2,'0') + ' ' + ([string]$Email.Date.Hour).PadLeft(2,'0') + '-' + ([string]$Email.Date.Minute).PadLeft(2,'0') + '-' + ([string]$Email.Date.Second).PadLeft(2,'0')))
       ("To: " + $Email.User) | Out-File ($FullFilePath + ".txt") -Append
       ("From: " + $Email.From.Address) | Out-File ($FullFilePath + ".txt") -Append
       ("Subject: " + $Email.Subject) | Out-File ($FullFilePath + ".txt") -Append
       $Email.Date.LocalDatetime | Out-File ($FullFilePath + ".txt") -Append
       $Email.TextBody | Out-File ($FullFilePath + ".txt") -Append
       If ($Email.Attachments -ne $null) {
           If (!(Test-Path $FullFilePath)) {
               New-Item -Path $FullFilePath -ItemType Directory
           }
           Get-GSGmailMessage -User $Recipient -Id $EmailList.Id -ParseMessage -SaveAttachmentsTo $FullFilePath
       }
   }
}

 

Owing to an SAR this morning, I've spent the past 2 hours developing and debugging this little snippet

 

Define 2 users, a date range, and a root folder, and using the PSGSuite module, this script will dump every email - and email attachment - into a folder of your choosing, exporting each email as YYYY-MM-DD HH-MM.txt and putting each attachment into an identically-named YYYY-MM-DD HH-MM subfolder.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...