我遇到了一个奇怪的情况,我无法弄清楚问题所在。我想要获取给定路径的所有子目录的 ACL 和 AccessRules。如果我单独执行它们,我不会收到任何错误
gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | Format-List | Out-File C:\temp\permission.txt
gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | foreach {$_.GetAccessRules($true, $true, [System.Security.Principal.NTAccount])} | Out-File C:\temp\permission1.txt -Append
但是,我想在 foreach 循环中执行此操作,以更好地操作和处理文件的生成方式。这是我在脚本中尝试做的事情:
sl c:\test_folder
gci * -Recurse | Export-Csv c:\temp\dir.csv -Force
$pastas = gci -Recurse | where {$_.PsIsContainer}
if (Test-Path C:\temp\permission.txt)
{
ri c:\temp\permission.txt
}
foreach ($pasta in $pastas)
{
$pasta
Test-Path $pasta
$acl = get-acl $pasta
$acl | format-list | Out-File -FilePath c:\temp\permission1.txt -Append
$acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) | Out-File -FilePath c:\temp\permission1.txt -Append
}
当我foreach
直接在$pastas
变量的根目录中处理文件夹时,Test-Path
返回true
. 然而,在第一个子目录中,Test-Path
返回false
,但该文件夹确实存在。在 get-acl 上,我得到 PathNotFound 异常:
Get-Acl : Não é possível localizar o caminho 'Exportacao' porque ele não existe.
Em C:\Temp\Script Get Info.ps1:12 caractere:17
+ $acl = get-acl <<<< $pasta
+ CategoryInfo : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
+ FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
我究竟做错了什么?