Avatar of simpleworksit
simpleworksit

asked on 

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!
ExchangeWindows Server 2008Powershell

Avatar of undefined
Last Comment
SubSun
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of simpleworksit
simpleworksit

ASKER

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?
Avatar of SubSun
SubSun
Flag of India image

Copy all the commands and paste it in to a file save with .ps1 extension..
Avatar of simpleworksit

ASKER

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?
Avatar of SubSun
SubSun
Flag of India image

What is the contents of RetentionPolicies-Archives.ps1?
Avatar of simpleworksit

ASKER

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
Avatar of SubSun
SubSun
Flag of India image

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..
Avatar of simpleworksit

ASKER

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 ‘
Avatar of simpleworksit

ASKER

‘ works and ' doesn't
Avatar of SubSun
SubSun
Flag of India image

Hmm.. not sure about it.. both quotes marks are working for me..
Avatar of SubSun
SubSun
Flag of India image

Or probably Powershell taking it as ` (backtick), which wont work..
Avatar of simpleworksit

ASKER

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
Avatar of simpleworksit

ASKER

Yeah I believe powershell and task scheduler were taking it as a backtick
Avatar of SubSun
SubSun
Flag of India image

Try..
Get-Mailbox -ResultSize unlimited -Database Tier1 | ? {$_.ArchiveDatabase -ne "Tier1"}

Open in new window

Avatar of simpleworksit

ASKER

That worked.  What does ? mean after the pipe
Avatar of SubSun
SubSun
Flag of India image

? is alias for where-object
Avatar of simpleworksit

ASKER

Not sure why where wasn't working then, but that worked. Thank you so much for all your help!  You've been great!
Avatar of SubSun
SubSun
Flag of India image

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..
Avatar of simpleworksit

ASKER

Oh I see.

Any idea why the filter wasn't working with -and
Avatar of SubSun
SubSun
Flag of India image

Not all attributes are filterable, you can search for the MS article to find the filterable parameters..
Avatar of simpleworksit

ASKER

Gotcha.  I thought where and -filter were pretty much the same thing.  Goes to show how much I have to learn.
Avatar of SubSun
SubSun
Flag of India image

Filterable Properties for the -Filter Parameter
http://technet.microsoft.com/en-us/library/bb738155%28v=exchg.150%29.aspx
Exchange
Exchange

Exchange is the server side of a collaborative application product that is part of the Microsoft Server infrastructure. Exchange's major features include email, calendaring, contacts and tasks, support for mobile and web-based access to information, and support for data storage.

213K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo