Passing Array to Function is not working as expected

by Oct 6, 2013

Hey,

I'm trying to pass an array to another function and process it to return something found therein.

Here's the code:

 

[array]$MainItems = @("Main1","Main2","Main3")
[array]$SubItems = @("Sub1","Sub2","Sub3","Sub4")
$ItemCount = 0

Function Process-SubItems ($ItemCount, $Item, $NewArray) {
write-host $ItemCount`n
write-host $Item`n
foreach ($Item in $NewArray) {$Item}
}

Function Process-MainItems {
foreach ($Item in $MainItems) {
$ItemCount++
Write-host "An Item was found:"$Item`n
$NewArray = $SubItems
Process-SubItems ($ItemCount, $Item, $NewArray)
}
}

It doesn't seem to matter what I do in Process-SubItems, $Item always outputs System.Object[].

What am I doing wrong?

 

Thanks!