【发布时间】:2011-06-26 17:13:35
【问题描述】:
我正在尝试编写一个递归函数,该函数将返回数组中的信息,但是当我将 return 语句放入函数时,它会丢失某些条目。
我正在尝试以递归方式查看指定深度的文件夹,以获取与文件夹关联的 acl。我知道 getChildItem 有一个递归选项,但我只想逐步浏览 3 级文件夹。
下面的代码摘录是我一直用来测试的。如果在没有返回语句的情况下调用 getACLS(在下面注释掉),结果是:
文件夹 1
文件夹 12
文件夹 13
文件夹 2
当使用 return 语句时,我得到以下输出:
文件夹 1
文件夹 12
所以看起来返回语句正在退出递归循环?
我的想法是我想返回一个多维数组,例如 [文件夹名称、[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
【问题讨论】:
标签: function powershell recursion return