Powershell Script versus Exchange Management Shell

I recently received a new workstation, windows 8.1 at work and am trying to get some Exchange powershell scripts to run on it that I wrote on my Windows 7 box.

Here's an example of the problem:

In Exchange Management Shell I attempt:

get-mailbox -organizationalunit sales -resultsize unlimited | Where-Object {$_.LitigationHoldEnabled -eq $true} | Get-MailboxFolderStatistics

If I run that same command in a Powershell window after running: 

Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010

I get:

WARNING: An unexpected error has occurred and a Watson dump is being generated: Operation is not valid due to the current state of the object.
Get-MailboxFolderStatistics : Operation is not valid due to the current state of the object.
At line:1 char:115
+ ... d -eq $true} | Get-MailboxFolderStatistics
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MailboxFolderStatistics], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Exchange.Management.Tasks.GetMailboxFolderStatistics

Any ideas?

August 14th, 2015 10:16am

Ive ran into this problem many times. But it is very specific Exchange Management Shell tasks. These commands will run perfectly in EMS but NOT Powershell. I have found these will run properly if ran FROM an Exchange server where Exchange is truly installed AND/OR you can launch EMS within powershell with the below....

$Exserver=Get-Exchanegserver <enter exchange server here>

$ExPath="$env:ExchangeInstallPath" + "bin\RemoteExchange.ps1" . $ExPath ; Connect-ExchangeServer -Server $ExServer -clientapplication:ManagementShell


So, add the above two lines into the top of your script/ code and it will open EMS withing your ISE/ powershell window.



  • Edited by Josh Lavely 16 hours 35 minutes ago Spelling
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 10:43am

Directly loading the PowerShell snappin is not supported.

http://blogs.technet.com/b/rmilne/archive/2015/01/28/directly-loading-exchange-2010-or-2013-snapin-is-not-supported.aspx

From that post you will need to do something like:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos 

Import-PSSession $Session

August 14th, 2015 1:16pm

Thanks for the input, unfortunately I am still getting the same error.

Side note, I added the three lines you suggested after the

Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010

statement in my script.  It is connecting to the server I spec

Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 2:00pm

Thanks for the input, unfortunately I am still getting the same error.

Side note, I added the three lines you suggested after the

Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010

statement in my script.  It is connecting to the server I spec

August 14th, 2015 2:04pm

Ive ran into this problem many times. But it is very specific Exchange Management Shell tasks. These commands will run perfectly in EMS but NOT Powershell. I have found these will run properly if ran FROM an Exchange server where Exchange is truly installed AND/OR you can launch EMS within powershell with the below....

$Exserver=Get-Exchanegserver <enter exchange server here>

$ExPath="$env:ExchangeInstallPath" + "bin\RemoteExchange.ps1" . $ExPath ; Connect-ExchangeServer -Server $ExServer -clientapplication:ManagementShell


So, add the above two lines into the top of your script/ code and it will open EMS withing your ISE/ powershell window.



  • Edited by Josh Lavely Friday, August 14, 2015 2:52 PM Spelling
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 2:41pm

Ive ran into this problem many times. But it is very specific Exchange Management Shell tasks. These commands will run perfectly in EMS but NOT Powershell. I have found these will run properly if ran FROM an Exchange server where Exchange is truly installed AND/OR you can launch EMS within powershell with the below....

$Exserver=Get-Exchanegserver <enter exchange server here>

$ExPath="$env:ExchangeInstallPath" + "bin\RemoteExchange.ps1" . $ExPath ; Connect-ExchangeServer -Server $ExServer -clientapplication:ManagementShell


So, add the above two lines into the top of your script/ code and it will open EMS withing your ISE/ powershell window.



  • Edited by Josh Lavely Friday, August 14, 2015 2:52 PM Spelling
August 14th, 2015 2:41pm

Using Remote Powershell did work, thank you.

Any explanation for it not working via direct powershell?  Just trying to understand if this is something that I'm doing wrong or

Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 4:58pm

Good to hear that it's working. 

Directly loading the Exchange 2010/2013 PSSnapin is not supported, we would even start to troubleshoot for something like this.

Please refer to the post I added above for some more details, and keep on using remote PowerShell for Exchange 2010/2013.

August 16th, 2015 11:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics