Remotely run clean-mailboxdatabase command?

I'm trying to remotely execute the clean-mailboxdatabase command from a remote server by using the following syntax in a .ps1 file:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Authentication Kerberos
Import-PSSession $s
clean-mailboxdatabase -identity "ServerName\MailboxStoreName"

I call the above .ps1 file using the following batch file:
Powershell.exe -executionpolicy bypass -File  C:\CleanUpMail.ps1

The error I get is:
The operation couldn't be performed because object 'ServerName\MailboxStoreName' couldn't be found on 'DomainControllerName.Domain.Com'.
    + CategoryInfo          : NotSpecified: (0:Int32) [Clean-MailboxDatabase],
    + FullyQualifiedErrorId : 2458494B,Microsoft.Exchange.Management.SystemCon

I'm not sure why it's looking for the mailbox stores on the DC. I use a similar process to issue the following syntax without issue:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Authentication Kerberos
Import-PSSession $s
Move-ActiveMailboxDatabase 'MailboxStoreName' -ActivateOnServer TargetServer -Confirm:$false

I've tried the following combinations:
clean-mailboxdatabase -identity "ServerName\MailboxStoreName"
clean-mailboxdatabase -identity "ServerName\Master\MailboxStoreName"
clean-mailboxdatabase "MailboxStoreName"

Please advise! Exchange 2010.
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.

Adam FarageEnterprise ArchCommented:
From technet:

The Identity parameter specifies the mailbox database that you want to update. You can use one of the following values:


    Distinguished name (DN)

    Name of the mailbox database

Run Get-MailboxDatabase and grab the GUID. Throw all of them into a CSV file and run Import-CSV along with the ForEach (%) switch to run against all databases.
Abdul Khadja AlaoudineCommented:
Are you running it in Exchange Management Shell on remote server?
Why are you adding in all the additional work and making this so hard? Just do the following:

1. Open EMS (Exchange Management Shell) from a workstation where you have the Exchange Management Tools installed.
2. Type the following Clean-MailboxDatabase <mailboxdatabasename>

You are done.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

ablsysadminAuthor Commented:
Get-MailboxDatabase does not return a GUID, it returns Name, Server, Recovery, ReplicationType.

We're not running Exchange Management Shell on the remote server, but we're importing the Exchange commands into the remote powershell session using:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Authentication Kerberos
Import-PSSession $s

Yes, Clean-MailboxDatabase <mailboxdatabasename> is easy, but running it manually on a server with Exchange Management Tools is not what we're trying to achieve here.
Why not? It is the accepted and supported way of doing it? You wouldn't want to do it any other way as it is just complicating an already easy process.
ablsysadminAuthor Commented:
The idea is to automate it remotely as that is the requirement presented to us. We'd really just like to know if it's possible to achieve this. If it's not, we'll obviously go back to the method you described. Thank you for constant feedback.
You can still automate it remotely but with the right tools installed on the machine. It's the best/easiest way. Good luck.

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

From novice to tech pro — start learning today.

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.