views:

39

answers:

3

I'm getting a specified cast error trying to return a integer, example code below:

results = pipeline.Invoke();

foreach (PSObject ps in results)
{
int diff = 0;
Int32 exchcount = Convert.ToInt32(ps.Members["itemcount"].Value);
diff = itemcount - exchcount;
}

I'm trying to find out what the data type if for itemcount of the Get-Mailboxstatistics but i can't find the information, i assume it's Int, i've tried string but that does not work either. any ideas please let me know!

Thanks Steve

A: 

You can put breakpoin in the foreach cycle and see what type is ps.Members["itemcount"].Value (e.g. you can do ps.Members["itemcount"].Value.GetType() in the Immidiate window).

Andrew Bezzub
A: 

The error you are getting is at the foreach loop. The variable ps is declared of type PSObject while results might be something else (an array of object which are not of the correct type). Look at the type of the results variable.

Darin Dimitrov
A: 

i don't think it's the foreach statement, if i use a string type value like 'DisplayName' cmd value it works.. only for a value/int type it seems to fail. i'll try and check the break point to see the value type..

Thanks! ST