Solved

Need help with Exchange 2010 powershell syntax

Posted on 2013-06-27
3
554 Views
Last Modified: 2013-06-27
I use a script that exports every mailbox to a PST for temporary backup reasons This currently works:

foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -ContentFilter {(Received -lt "04/21/2013")} -FilePath "\\filesserver\PST\$($i.Alias).pst" }


I am trying to figure out a way so I don't have to manually change the date in the received later than switch. Just testing, I was able to get this to work:

$date=get-date
$startdate=$date.addmonths(-3)
New-MailboxExportRequest -mailbox testuser-contentfilter "Received -gt '$startdate'" -filepath \\fileserver\PST\testuser.pst

So I tried this script:

$date=get-date
$startdate=$date.addmonths(-3)
foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -ContentFilter {(Received -lt '$startdate')} -FilePath "\\fileserver\ExmergePST\$($i.Alias).pst" }

I get this error:

The provided ContentFilter value is invalid. ContentFilter is invalid. The value "$startdate" could not be converted to
 type System.DateTime. --> The value "$startdate" could not be converted to type System.DateTime.
    + CategoryInfo          : InvalidArgument: ((Received -lt '$startdate'):String) [], ContentFilterInvalidPermanentE
   xception
    + FullyQualifiedErrorId : DD0301EC

 I've tried different syntax changing the delimeters with no success. Any help would be gretly appreciated. Thanks
0
Comment
Question by:gwbmcse
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39281913
Remove the single quote and try..
-ContentFilter {(Received -lt $startdate)}

Open in new window

Single quotes will not expand variables in PowerShell, double quotes will..
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39281968
Further, the difference between
  -ContentFilter {(Received -lt $startdate)}
and
  -ContentFilter {(Received -lt "$startdate")}
is that the latter will use a string representation of the date - which might lead to errors or unwanted behaviour (the rules for implicit type transformations are complex). The former allows to provide the var contents as DateTime object, which is best.
0
 

Author Closing Comment

by:gwbmcse
ID: 39282060
That did the trick
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
how to add IIS SMTP to handle application/Scanner relays into office 365.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now