Powershell Returns Recursion

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 ($newPath, $security, getACLS $newPath $max ($current+1))
   #            getACLS $newPath $max ($current+1)
                return getACLS $newPath $max ($current+1)
            }   
        }
    } elseif ($current -eq $max ) {
        Write-Host max
        return ($path, $security)
    }
}

$results = getACLS "PATH\Testing" 2 0
+5
3

. foreach, . foreach, if.

function getACLS ([string]$path, [int]$max, [int]$current) {

$dirs = Get-ChildItem -Path $path | Where { $_.psIsContainer }
$acls = Get-Acl -Path $path
$security = @()
$results = @()

foreach ($acl in $acls.Access) {
    $security += ($acl.IdentityReference, $acl.FileSystemRights)
}   

if ($current -lt $max) {
    if ($dirs) {
        foreach ($dir in $dirs) {
            $newPath = $path + '\' + $dir.Name
            $next = $current + 1
            $results += (getACLS $newPath $max $next)
        }   
    } else {
        $results = ($path, $security)
    }
    return ($path, $security, $results)
} elseif ($current -eq $max ) {
    return ($path, $security)
}
}
+7

return, - . PowerShell, . , PowerShell -. :

return 1,2

:

1,2
return

, , ( $null), . , :

function recursive($path, $max, $level = 1)
{
    $path = (Resolve-Path $path).ProviderPath
    Write-Host "$path - $max - $level"
    foreach ($item in @(Get-ChildItem $path))
    {
        if ($level -eq $max) { return }

        recursive $item.PSPath $max ($level + 1) 
    }
}

recursive ~ 3
+3

: . , . .

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 -lt $max) {
        if ($dirs) {
            foreach ($dir in $dirs) {
                $newPath = $dir.FullName
                $security
                getACLS $newPath $max ($current+1)
            }   
        }
    } elseif ($current -eq $max ) {
        Write-Host max
        return $security
    }
}

$results = getACLS "C:\Scripts" 2 0

, . GetACLs. , , $security . ACL $results. if ($ current -lt $max). , ($ current -le $max).

, , . .

========================================== OLD ==== ========================================= .

, , .

PS Custom . ,

function GetItem {
$itemsArray = @()
Get-ChildItem C:\Scripts | ForEach-Object {
    $itemsObject = New-Object PSObject
    Add-Member -InputObject $itemsObject -MemberType NoteProperty -Name "FullName" -Value $_.FullName
    Add-Member -InputObject $itemsObject -MemberType NoteProperty -Name "Name" -Value $_.Name
    $itemsArray += $itemsObject
}
return $itemsArray
}

Thus, you can return the object as soon as it is completely built, with the necessary information.

+2
source

All Articles