I'm using the GMail.PS Module found here and can't get it to properly filter emails with the -after or -on switch. I tried this on Powershell 2 and Powershell 4 on Windows Server 2008 and Windows 7 using different PCs.
This is a sample code:
$EmailUser = 'Test@GMail.Com' $EmailUser2 = 'Test@Test.Com' $EmailSubject = "isAlive" $pw = [System.Convert]::FromBase64String("Base64Password") $pw = [System.Text.Encoding]::UTF8.GetString($pw) $secpasswd = ConvertTo-SecureString $pw -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ($EmailUser, $secpasswd) $gmail = New-GmailSession -Credential $mycreds $inbox = $gmail | Get-Mailbox $inbox | Get-Message -Before "2014-11-30" ## This works fine. $inbox | Get-Message -After "2014-11-30" ## This does not work.
I looked through the module and saw that the -after command is using
if ($On) { $imap += 'ON "' + $(GetRFC2060Date $After) + '"'
if ($After) { $imap += 'AFTER "' + $(GetRFC2060Date $After) + '"'
if ($Before) { $imap += 'BEFORE "' + $(GetRFC2060Date $Before) + '"'
As we can see they are identical here.
and function GetRFC2060Date is:
function GetRFC2060Date([DateTime]$date) { $date.ToString("dd-MMM-yyyy hh:mm:ss zz", [CultureInfo]::GetCultureInfo("en-US"))
Which doesn't really give me any info as to why it's not working.
Using -After gives us this error:
"Exception calling "Search" with "1" argument(s): "xm003 BAD Could not parse command"
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Gmail.ps\Gmail.ps.psm1:344 char:5
+ $result = $Session.Search('(' + $criteria + ')');
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Exception
"
Using -On gives us this error which gives us more info and points to the GetRFC2060Date function.
"
GetRFC2060Date : Cannot process argument transformation on parameter 'date'. Cannot convert null to type "System.DateTime".At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Gmail.ps\Gmail.ps.psm1:276 char:44
+ $imap += 'ON "' + $(GetRFC2060Date $After) + '"'
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [GetRFC2060Date], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,GetRFC2060Date"