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
Subash Sundharan

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Subash Sundharan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
simpleworksit

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?
Subash Sundharan

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

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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Subash Sundharan

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

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
Subash Sundharan

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..
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
simpleworksit

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 ‘
ASKER
simpleworksit

‘ works and ' doesn't
Subash Sundharan

Hmm.. not sure about it.. both quotes marks are working for me..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Subash Sundharan

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

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
ASKER
simpleworksit

Yeah I believe powershell and task scheduler were taking it as a backtick
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Subash Sundharan

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

Open in new window

ASKER
simpleworksit

That worked.  What does ? mean after the pipe
Subash Sundharan

? is alias for where-object
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
simpleworksit

Not sure why where wasn't working then, but that worked. Thank you so much for all your help!  You've been great!
Subash Sundharan

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..
ASKER
simpleworksit

Oh I see.

Any idea why the filter wasn't working with -and
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Subash Sundharan

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

Gotcha.  I thought where and -filter were pretty much the same thing.  Goes to show how much I have to learn.
Subash Sundharan

Filterable Properties for the -Filter Parameter
http://technet.microsoft.com/en-us/library/bb738155%28v=exchg.150%29.aspx
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck