Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

mailbox search

$
0
0

Hi All

I am trying to create a monthly windows schedule task where mailbox deleted items are searched for last 30 days from the date the schedule is run.

This works
Search-Mailbox -Identity "user" -SearchDumpsteronly -SearchQuery {"Received: 04/01/15..04/02/15 "} -includeunsearchableitems -TargetMailbox searchresults -TargetFolder "test" -Loglevel Full

As i schedule task i do not want to add manual entry {"Received: 04/01/15..04/02/15 "} each time it has to run.


I have tried this and it has failed  

$start=(Get-Date).adddays(-30)
$end=(Get-Date).adddays(-1)
$start = “{0:dd/MM/yyyy} –f $start
$end = “{0:dd/MM/yyyy} –f $end
 
Search-Mailbox -Identity "user" -SearchDumpsteronly -SearchQuery  {("Received -lt $end") -and ("Received -gt $start")} -includeunsearchableitems -TargetMailbox searchresults -TargetFolder "test" -Loglevel Full

Also replaced {("Received -lt $end") -and ("Received -gt $start")} with
{received:$start..$end}

I have come to realise that searchquery does not like varibles date  e.g. $start and $end, if i add real dates 1/1/15 works fine. I have looked at $start and $end values and they appear to match the value that works e.g. 1/1/15

Would anyone know why searchquery behaves like this with date or how to fix it or if i am just doing something wrong

thanks

Ps apologies i did add something similar in an incorrect forum but have done a bit more testing since then based on information provided


Viewing all articles
Browse latest Browse all 6937

Trending Articles