Exchange Powershell script gives dr Watson

Hi,

When running a powershell (scheduled) through a batchfile I get a dr. Watson error:

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-ActiveSyncDeviceStatistics : Operation is not valid due to the current
state of the object.
At D:\Scripts\ActiveSyncExport.ps1:34 char:27
+ $Devices = $Mailboxes | %{Get-ActiveSyncDeviceStatistics -Mailbox
$_.Identity} | ...
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ActiveSyncDeviceStatistic
   s], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Excha
   nge.Management.Tasks.GetMobileDeviceStatistics

But when I run this script from the Exchange Management Shell manually it works normally.

I tried this in the script, but that doesn't work either:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://casserver.ourdomain.com/PowerShell/ -Authentication Kerberos
Import-PSSession $Session

The servers are running Exchange 2010 SP3 with rollup 2.

It looks like a sort of buffer overflow error.

Does anybody know a solution?

February 19th, 2014 3:36pm

I get these too on boxes that have the Exchange console installed, but aren't the CAS or mailbox servers themselves. I can't seem to find any solutions to this, but a lot of people are having this issue. It seems to only happen to a few powershell cmdlets.

I've read somewhere that it may be a .NET issue but not sure where to begin. Running Powershell 2 

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2014 9:42pm

Me too.. same problem, same "Get-ActiveSyncDeviceStatistics" command..  I've tried on several servers, and my scripts run fine on the Exchange servers themselves, just wont run on the servers we have designated as the ones to run scheduled / repetitive tasks..

No ideas??

February 25th, 2015 5:30pm

Hi Guys,

this is probably due to the usual serialization / deserialization issue. Try importing the commands with this function script.

Another note - try to avoid using Exchange Cmdlets in combination with ForEach-Object uses - go straight through the pipeline instead:

$mb = Get-Mailbox | Where { "Whatever Condition you need to apply" } | Select -ExpandProperty Identity | %{ $_.ToString() }
Get-ActiveSyncDevice | Where { $mbl -contains $_.UserDisplayName } | Get-ActiveSyncDeviceStatistics


Otherwise you'll exceed session limits, whether on the server or not.

Cheers,

Free Windows Admin Tool Kit Click here and download it now
February 27th, 2015 8:11am

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

Other recent topics Other recent topics