?
Solved

Need help with Exchange 2010 powershell syntax

Posted on 2013-06-27
3
Medium Priority
?
578 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 2000 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

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
In this video we show how to create a Shared Mailbox 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 >> Sha…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

770 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