SCAPaoT

System Center, Automation, Powershell and other Thoughts

Powershell: return is also an object…

I’m currently working on automation of SCCM software updates using Powershell.

I spend a lot of time on the following extremly simple task.
1. a function creates a list with updates
2. the same function returns the .count of the array of updates
3. the return value is added to an global variable of type Int32

And: Baaaahm -> System.Object[] can not be converted into System.Int32

Where the hell is the System.Object[] coming from? The return was an Int32!

See your self:

function returnTest
{
1..30
$array = 1..30
 return $array.count
 }

returnTest.gettype()

After I regocognized this behavoir of retrun, I found a very good blog about that:
http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!811.entry

So, make sure to throw away everything expect the return value with [void] or pipe to out-null.

As an alternativ, make sure return is the last command in the function.
So you can grab the value you expected from
$retrunvalue[$returnvalue.length-1] …

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

COMMENTS