我有一个奇怪的情况,我无法弄清楚问题所在。我希望获取给定路径的所有子目录的ACL和AccessRules。如果我单独做这些,我没有得到任何错误PowerShell路径不存在foreach脚本
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
我在做什么错?
Tks为你的时间和答案 – Pascal