Function not returning expected object

I have a strange situation with this PowerShell function. It is supposed to return an ArrayList object, but if the loop adds only 1 element to an ArrayList, the function returns an SPList element instead of an Expected ArrayList object. I am focused on why PowerShell behaves this way.

function Get-ContentOrganizerRules ( [System.String]$siteUrl = "http://some.sharepoint.url" ) { Write-Host -ForegroundColor Gray "Searching for Content Organizer Rules: " $siteUrl # ArrayList to hold any found DataConn Libs [System.Collections.ArrayList]$CORules = New-Object System.Collections.ArrayList($null) $lists = Get-SPWeb $siteUrl | Select -ExpandProperty Lists | Where { $_.GetType().Name -eq "SPList" -and $_.hidden } foreach($list in $lists) { #Write-Host $list ; foreach($contenType in $list.ContentTypes){ if($contenType -ne $null){ if($contenType.Id.ToString() -eq "0x0100DC2417D125A4489CA59DCC70E3F152B2000C65439F6CABB14AB9C55083A32BCE9C" -and $contenType.Name -eq "Rule") { $CORules.Add($list)>$null; Write-Host -BackgroundColor Green -ForegroundColor White "Content Organizer Rule found: " $list.Url>$null; } } } } return $CORules; } 

This is the call code:

 $CORulesResults = Get-ContentOrganizerRules $web.URL; if($CORulesResults.Count -gt 0){ $Results.AddRange($CORulesResults); } 
+6
source share
3 answers

There is an implicit pipeline there, and pipelines usually "deploy" arrays, collections, and arraylists to the same level.

Try the following:

 return ,$CORules 
+7
source

Or you can force the $ CORulesResult variable to an array with [Array] in front

 [Array]$CORulesResults = Get-ContentOrganizerRules $web.URL; if($CORulesResults.Count -gt 0){ $Results.AddRange($CORulesResults); } 
+1
source

I had a similar problem when I used [System.Collections.ArrayList] instead of regular fixed size arrays. The returned object was not the element of the array that I was hoping for, but the whole array, and it was barren, with the exception of one element that I wanted to return. Tell us how to ruin the stack.

The solution was simple: stopped using [System.Collections.ArrayList]

Here you can declare and process $ CORules.

 $CORules = @() ... $CORules = $CORules + $list 

Viva le Bash !

0
source

All Articles