Search code examples
xmlpowershellgmail

Gmail feed: Retrieve body from latest email. Powershell


I want to retrieve the body of the latest gmail, using their feed. Here's the code I have.

I'm also including the results of

$xml.feed.entry | Select *

Which is:

title           : 
summary         : Test body
link            : link
modified        : 2018-02-10T21:06:18Z
issued          : 2018-02-10T21:06:18Z
id              : tag:gmail.google.com,2004:1592049563135473902
author          : author
Name            : entry
LocalName       : entry
NamespaceURI    : http://purl.org/atom/ns#
Prefix          : 
NodeType        : Element
ParentNode      : feed
OwnerDocument   : #document
IsEmpty         : False
Attributes      : {}
HasAttributes   : False
SchemaInfo      : System.Xml.XmlName
InnerXml        : <OMITTED>
InnerText       : <OMITTED>
NextSibling     : 
PreviousSibling : modified
Value           : 
ChildNodes      : {title, summary, link, modified...}
FirstChild      : title
LastChild       : author
HasChildNodes   : True
IsReadOnly      : False
OuterXml        : <OMITTED>
BaseURI         : 
PreviousText    :

`

I'm using a function from dmitrysotnikov.wordpress.com to measure the latest datetime value (it works). I'm just having trouble telling it to select the body of the message with the newest date.

function Measure-Latest {
    begin { $latest = $null }
    process {
            if (($_ -ne $null) -and (($latest -eq $null) -or ($_ -gt 
$latest))) {
                $latest = $_ 
            }
    }
    end { $latest }
}

$webclient = new-object System.Net.WebClient

$webclient.Credentials = new-object System.Net.NetworkCredential 
("[email protected]", "PASSWORD")

[xml]$xml= 
$webclient.DownloadString("https://mail.google.com/mail/feed/atom")

$latestmail = $xml.feed.entry.issued | Measure-Latest

Solution

  • You're almost there! The final step is to specify you want the e-mail which has the $latestmail time-stamp as the issued property, and that you want the summary property of that e-mail:

    ($xml.feed.entry | Where-Object {$_.issued -eq $latestmail}).summary