Powershell to loop Managed Folder Assistant

Hi,

I'm trying to write a script so it'll look for mailboxes that are over a certain limit and force the MFA until the are under a certain limit, but when it calls the MFA it doesn't know how to resolve the Identity.

$mailboxes = Get-MailboxStatistics -Server "server" | where {$_.TotalItemSize -gt  2.5} | select name
$mailboxes | foreach {
$mailboxes > 2.5 ; do { Start-ManagedFolderAssistant -Identity $mailboxes} until ($TotalItemSize -lt 2)
}

Could you please point out what I'm doing wrong?

Thanks,
JAS_MarianoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There are a lot of issues with your code. This is what works:
$cont = $true
while ($cont)
{
  $cont = $false
  Get-MailboxStatistics -Server "server" |
     ? {$_.TotalItemSize -gt  2.5} |
     select -First 1 |
     Start-ManagedFolderAssistant  | 
     % { $cont = $true }
}

Open in new window

It does not use the lower limit to keep it simple. The way this code runs it should process the same mailbox until it is below the limit, and then process the next mailbox.

The point I'm not certain about is whethter the limit has the correct scale. I would expect it to be in bytes or Kb, but cannot look that up at the moment.
0
JAS_MarianoAuthor Commented:
Hi,

Thanks a lot for your comment, but when I tried that I get the attached error.

Thanks,
Cmdlet-error.JPG
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I should have said "should work" ;-)
$cont = $true
while ($cont)
{
  $cont = $false
  Get-MailboxStatistics -Server "server" |
     ? {$_.TotalItemSize -gt  2.5} |
     select -First 1 |
     % { 
       Start-ManagedFolderAssistant -identity $_.Name
       $cont = $true
     }
}

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

JAS_MarianoAuthor Commented:
Thanks, I really appreciate it but I'm still having the error I was having with my original script, for some reason it doesn't like the "-Identity". Please see attached.
Cmdlet-error-2.JPG
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is it Exchange 2010 without Service Pack 1, or Exchange 2007? Because:

In the Microsoft Exchange Server 2010 release to manufacturing (RTM) and Exchange Server 2007 versions, the Identity parameter specifies the Mailbox server to start the assistant and process all mailboxes on that server, and the Mailbox parameter specifies the mailbox to process. In Exchange 2010 Service Pack 1 (SP1) and later, the Mailbox parameter has been removed, and the Identity parameter accepts the mailbox or mail user to process.
https://technet.microsoft.com/en-us/library/aa998864(v=exchg.150).aspx

In that case we need to use -Mailbox instead.

On another note, I doubt calling the MFA in a loop will help. AFAIK it does nothing but to enforce Retention Policy processing. If there is nothing to remove anymore, but the mailbox is still oversize, it stays that way.
0
JAS_MarianoAuthor Commented:
It's Exchange 2013 with CU9, the reason I'm trying to loop the MFA is because we are having a situation where MFA starts process a mailbox and then it throws event ID 10065:

"The number of errors encountered while processing mailbox example@contoso.com has exceeded the maximum allowed limit of 10. Processing for this mailbox will stop and will be attempted again during the next schedule interval."

If I manually force the MFA a couple of times on that Mailbox it is able to finally move all the items to the archive mailbox (which is on the Cloud btw), so my idea is to build a script where I tell it to look for Mailbox sizes over 2.5 gb (we've set the limit to 3gb) and force the MFA until that Mbx is under 2 gb (I know that this will most likely be achieved because this mailboxes have an RTP to "MoveToArchive" of 15 days.
What I originally tried was to change the Workcycle schedule from 1 day to an hour or a couple of hours and it ran a whole weekend like that but for some reason it dnd't really did much.

Thanks,
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
It isn't the same error, btw. This time we do not have anything to provide, that is $_.Name is empty, and that is correct - there is no such property in the Get-MailboxStatistics results. We can either use the display name, or retrieve the mailbox and provide that to MFA:
$cont = $true
while ($cont)
{
  $cont = $false
  Get-MailboxStatistics -Server "server" |
     ? {$_.TotalItemSize -gt  2.5} |
     select -First 1 |
     get-mailbox |
     Start-ManagedFolderAssistant |
     % { $cont = $true }
}

Open in new window

0
JAS_MarianoAuthor Commented:
Hi,

It's now being able to excute the MFA but the bad news is that it's not actually looping it. I've adjusted the scale to bytes 2147483648 (2gb), and search for the first 10 mailboxes, it finds them and starts the MFA on each of them but once they throw the error it does not re-start the process again.
I can see on the event viewer the 9021 (MFA Starts) for each mbx and the 9022 (MFA Ends) along with the errors 10025 and 10065, but after it finishes it doesn't start again, If I re-execute the script it does start again and keeps moving items.

I've done the:

Get-MailboxStatistics -Server "server" | where {$_.TotalItemSize -gt 2147483648} | select displayname, totalitemsize

and I see it returns all values which "TotalItemSize" is greater than 2GB so I know it's finding exactly what I want.

Thanks,
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Does the script throw error messages in accordance to the eventlog entries?
0
JAS_MarianoAuthor Commented:
No, the script doesn't throw any errors. It seems that after it has found the first 10 and executed the MFA, it's done.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Start-ManagedFolderAssistant does not return anything, so setting $cont was not done, and hence no further iteration. Sigh.
$cont = $true
while ($cont)
{
  $cont = $false
  Get-MailboxStatistics -Server "server" |
     ? {$_.TotalItemSize -gt  2.5} |
     select -First 1 |
     get-mailbox |
     % { $cont = $true; $_ } | 
     Start-ManagedFolderAssistant
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JAS_MarianoAuthor Commented:
Hi,

This has been great! it's done the trick, I've modified it so it looks for the first 15 and sort it descending so it first hits the most critical ones, it seems that then when it loops it looks for the first 15 again and runs the MFA on those and so on. thanks a lot!!!

One more thing, could you please briefly describe the syntax?

Thanks!!
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Anything in particular? It is hard to start at zero ;-).
0
JAS_MarianoAuthor Commented:
I was curious how you are able to sort the part where the MFA does not return anything.
0
JAS_MarianoAuthor Commented:
Expert was great and answered every question and was watching this case very closely.
Thank you so much!
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The MFA does not need to return anything (and will never). I just wanted to use the result as a side-effect to set the $cont  var for doing the loop again. All I had to do to achieve this was to move the corresponding foreach loop up the pipe, executed before the MFA.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.