Solved

Need help with Exchange 2010 powershell syntax

Posted on 2013-06-27
3
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
In this video we show how to create an email address policy 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 Mail Flow…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

691 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