我遇到了一个奇怪的情况,我无法弄清楚问题所在。我想获取给定路径的所有子目录的 ACL 和访问规则。如果我单独做,我不会得到任何错误
gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | Format-List | Out-File C:temppermission.txt
gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | foreach {$_.GetAccessRules($true, $true, [System.Security.Principal.NTAccount])} | Out-File C:temppermission1.txt -Append
但是,我想在 foreach 循环中执行此操作,以更好地操作和处理文件的生成方式。以下是我尝试在脚本中执行的操作:
sl c:test_folder
gci * -Recurse | Export-Csv c:tempdir.csv -Force
$pastas = gci -Recurse | where {$_.PsIsContainer}
if (Test-Path C:temppermission.txt)
{
ri c:temppermission.txt
}
foreach ($pasta in $pastas)
{
$pasta
Test-Path $pasta
$acl = get-acl $pasta
$acl | format-list | Out-File -FilePath c:temppermission1.txt -Append
$acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) | Out-File -FilePath c:temppermission1.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:TempScript Get Info.ps1:12 caractere:17
+ $acl = get-acl <<<< $pasta
+ CategoryInfo : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
+ FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
我做错了什么?
sl c:test_folder
gci * -Recurse | Export-Csv c:tempdir.csv -Force
$pastas = gci -Recurse | where {$_.PsIsContainer}
if (Test-Path C:temppermission.txt)
{
ri c:temppermission.txt
}
foreach ($pasta in $pastas)
{
$pasta.FullName
Test-Path $pasta.FullName
$acl = get-acl $pasta.FullName
$acl | format-list | Out-File -FilePath c:temppermission1.txt -Append
$acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) | Out-File - FilePath c:temppermission1.txt -Append
}
尝试上面的脚本。
代码失败的原因是$pasta是一个 FileInfo 对象,并且测试路径需要一个具有完整路径的字符串。
尝试:
Test-Path $pasta.fullname
$acl = get-acl $pasta.fullname