• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

Exchange 2010 script help

I am trying to write a script that enables online archive and sets a retention policy for users in two mailbox databases and removes online archives and sets a different retention policy for users in another mailbox database.  

I have worked out the commands that I need, I just need some help putting them all together into a script and calling that script from a scheduled task.  I have tested running them from EMS and they work great. I would like some pointers on how to call them in EMS environment instead of regular powershell if possible.  If I run them from Powershell ISE or just powershell with the exchange snapins added, I get a bunch of warnings  Ex. WARNING: The cmdlet extension agent with the index 3 has thrown an exception in OnComplete()  

Here is what I have so far.

Load Exchange Modules/PSSnapins
Get-PSSnapin -Registered | Add-PSSnapin

Set Retention Policy based on Mailbox Database
Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Archive"
Get-Mailbox -ResultSize unlimited -Database Tier2 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Archive"
Get-Mailbox -ResultSize unlimited -Database Tier3 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Delete"

Enable/Disable Online Archive based on Mailbox Database
Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(Name -NotLike "DiscoverySearch*")} | Enable-Mailbox -Archive -ArchiveDatabase Tier1
Get-Mailbox -ResultSize unlimited -Database Tier2 -Filter {(Name -NotLike "DiscoverySearch*")} | Enable-Mailbox -Archive -ArchiveDatabase Tier2
Get-Mailbox -ResultSize unlimited -Database Tier3 | Disable-Mailbox -Archive


Any assistance would be greatly appreciated.  Thanks gentlemen!
0
simpleworksit
Asked:
simpleworksit
  • 11
  • 11
1 Solution
 
SubsunCommented:
Check this article for details..
http://consulting.risualblogs.com/blog/2010/11/04/running-exchange-2010-scripts-from-task-scheduler/

There are many options, other option is by using exchange PowerShell Console file..

PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command ". 'c:\script\EXScript.ps1'"

Open in new window

0
 
simpleworksitAuthor Commented:
Awesome, thank you very much.  This looks like it will execute as EMS instead of powershell with Exchange Snapins which should resolve the warnings. I will test shortly.

Is there anything i need to do with my commands to in the .ps1 script or just place them 1 per line?
0
 
SubsunCommented:
Copy all the commands and paste it in to a file save with .ps1 extension..
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
simpleworksitAuthor Commented:
Okay, so I can run my .ps1 file from EMS and it works mostly.  Need to add some lines to account for mailboxes who already have online archives and online archives in the wrong database.  However, I cannot get it run using a scheduled task (even running the task manually).  

The task says completed successfully, but does nothing as far as I can tell. So I opened a command prompt and i get
The string is missing the terminator: '.

powershell.exe -command ". ‘c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; E:\Scripts\RetentionPolicies-Archives.ps1"

Am I missing something?
0
 
SubsunCommented:
What is the contents of RetentionPolicies-Archives.ps1?
0
 
simpleworksitAuthor Commented:
For now (see below). I will add some and clean it up once I can get this working.

Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Archive" -Confirm:$False
Get-Mailbox -ResultSize unlimited -Database Tier2 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Archive" -Confirm:$False
Get-Mailbox -ResultSize unlimited -Database Tier3 -Filter {(Name -NotLike "DiscoverySearch*")} | Set-Mailbox -RetentionPolicy "6 Month Delete" -Confirm:$False

Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(Name -NotLike "DiscoverySearch*")} | Enable-Mailbox -Archive -ArchiveDatabase Tier1 -Confirm:$False
Get-Mailbox -ResultSize unlimited -Database Tier2 -Filter {(Name -NotLike "DiscoverySearch*")} | Enable-Mailbox -Archive -ArchiveDatabase Tier2 -Confirm:$False
Get-Mailbox -ResultSize unlimited -Database Tier3 | Disable-Mailbox -Archive -Confirm:$False
0
 
SubsunCommented:
Ok it looks good, don't see any this might have caused the error you mentioned.. Try running single command in that script and see if you get same error..
0
 
simpleworksitAuthor Commented:
It runs with the single line or all of the lines when I cut and pasted from this page as opposed to from my notes.  It appears the difference is the ' vs ‘
0
 
simpleworksitAuthor Commented:
‘ works and ' doesn't
0
 
SubsunCommented:
Hmm.. not sure about it.. both quotes marks are working for me..
0
 
SubsunCommented:
Or probably Powershell taking it as ` (backtick), which wont work..
0
 
simpleworksitAuthor Commented:
I just cut and pasted from my previous post and it worked fine, but when I manually typed ' or cut and pasted from my notepad it didn't.   It is working now from cmd.exe or from scheduled task (when task is run manually) with all of the lines. I'm scheduling the task to test that as well.  

The only thing I really need now is to have it move an already enabled online archive if it is in the wrong database. I cannot seem to get the filter to work with ArchiveDatabase.  Any thoughts?  You're getting the points regardless.  

What I have is
Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(Name -NotLike "DiscoverySearch*") -and (ArchiveDatabase -ne "Tier1")}   but it returns all mailboxes in the database.   I have tried  Get-Mailbox -ResultSize unlimited -Database Tier1 -Filter {(ArchiveDatabase -ne "Tier1")} as well and event Get-Mailbox -ResultSize unlimited -Database Tier1 | where-object {$_.ArchiveDatabase -ne $Tier1} as well and they all return all mailboxes in the database
0
 
simpleworksitAuthor Commented:
Yeah I believe powershell and task scheduler were taking it as a backtick
0
 
SubsunCommented:
Try..
Get-Mailbox -ResultSize unlimited -Database Tier1 | ? {$_.ArchiveDatabase -ne "Tier1"}

Open in new window

0
 
simpleworksitAuthor Commented:
That worked.  What does ? mean after the pipe
0
 
SubsunCommented:
? is alias for where-object
0
 
simpleworksitAuthor Commented:
Not sure why where wasn't working then, but that worked. Thank you so much for all your help!  You've been great!
0
 
SubsunCommented:
In your code where-object {$_.ArchiveDatabase -ne $Tier1}

Where $Tier1 is shows as variable (Start with $). Also it is not defined in the script.. so the value will be null..

So you are looking for the mailboxes for which archive database is not equal to null.. So it will return all mailboxes..
0
 
simpleworksitAuthor Commented:
Oh I see.

Any idea why the filter wasn't working with -and
0
 
SubsunCommented:
Not all attributes are filterable, you can search for the MS article to find the filterable parameters..
0
 
simpleworksitAuthor Commented:
Gotcha.  I thought where and -filter were pretty much the same thing.  Goes to show how much I have to learn.
0
 
SubsunCommented:
Filterable Properties for the -Filter Parameter
http://technet.microsoft.com/en-us/library/bb738155%28v=exchg.150%29.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now