I am trying to write a recursive function that will return information in an array, however, when I put the return statement in the function, it skips certain entries.
I am trying to recursively look at the specified folder depth, getting the acl associated with this folder. I know that getChildItem has a recurse option, but I only want to go through 3 levels of folders.
Below is a snippet of code that I used for testing. When getACLS is called without a return statement (see 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 going out of a recursive loop?
, , [ , [acls], [[, [], [[...]]]]] ..
cls
function getACLS ([string]$path, [int]$max, [int]$current) {
$dirs = Get-ChildItem -Path $path | Where { $_.psIsContainer }
$acls = Get-Acl -Path $path
$security = @()
foreach ($acl in $acls.Access) {
$security += ($acl.IdentityReference, $acl.FileSystemRights)
}
if ($current -le $max) {
if ($dirs) {
foreach ($dir in $dirs) {
$newPath = $path + '\' + $dir.Name
Write-Host $dir.Name
return getACLS $newPath $max ($current+1)
}
}
} elseif ($current -eq $max ) {
Write-Host max
return ($path, $security)
}
}
$results = getACLS "PATH\Testing" 2 0