我想寫一個遞歸函數,它將返回數組中的信息,但是當我將一個return語句放入函數中時,它會遺漏某些條目。Powershell遞歸返回
我想通過指定深度的文件夾遞歸地查看獲取與文件夾相關的acl文件夾。我知道getChildItem有一個遞歸選項,但我只想穿過3級文件夾。
下面的代碼片段是我一直用來測試的。當getACLS被不帶return語句(下面註釋)的結果是:
文件夾1
文件夾12
文件夾13
文件夾2
當返回語句是使用我得到以下輸出:
文件夾1
文件夾12
因此它看起來像return語句退出遞歸循環?
的想法是,我想返回多維數組如[文件夾名稱,[訪問控制列表],[子文件夾,[權限],[[...]]]]]等
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
不確定這是否會工作,因爲我需要嵌套對象。 – Rumpleteaser 2011-02-14 06:16:36