Powershell Recursion with Return

Question:

I am trying to write a recursive function that will return information in an array, however when I put a return statement into the function it misses certain entries.

I am trying to recursively look through a specified depth of folders getting the acl’s associated with the folder. I know getChildItem has a recurse option, but I only want to step through 3 levels of folders.

The excerpt of code below is what I have been using for testing. When getACLS is called without a return statement (commented out below) the results are:

Folder 1

Folder 12

Folder 13

Folder 2

When the return statement is used I get the following output:

Folder 1

Folder 12

So it looks like the return statement is exiting out from the recursive loop?

The idea is that I want to return a multidimensional array like [folder name, [acls], [[subfolder, [permissions],[[…]]]]] etc.

Answer:

The problem was the location of the return. I had it inside the foreach loop, meaning it was trying to return multiple times in the one function. I moved it outside the foreach, into the if statement instead.

Source:

Powershell Recursion with Return by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply