ewa*_*all 7 powershell acl try-catch
我有一个PowerShell脚本,它在for循环中使用GetChildItem通过文件系统递归.随着它的传播,它正在修复它找到的ACL问题(大多数人已经阻止了BUILTIN\Administrators帐户)...但是有一些它无法处理它自己,就像我得到[System.UnauthorizedAccessException]那样是一个明确的"拒绝"ACE.
代码行如下所示:
foreach($file in Get-ChildItem $dirRoot -Recurse -ErrorAction Continue) {
...
}
Run Code Online (Sandbox Code Playgroud)
当它在无法读取的路径上发现时,它会给出以下异常:
Get-ChildItem:拒绝访问路径"C:\ TEMP\denied".在Fix-ACLs.ps1:52 char:31 + foreach(Get-ChildItem中的$ file <<<< $ dirRoot -Recurse -ErrorAction Continue){+ CategoryInfo:PermissionDenied:(C:\ TEMP\denied:String)[获取-ChildItem],未经授权的AccessException + FullyQualifiedErrorId:DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
我想尝试/捕获或捕获错误,以便我可以修复ACL(即,删除"拒绝")就地,并且 - 最重要的是 - 继续循环而不会丢失我的位置.对我有什么建议吗?
你有没有使用过?
foreach($file in Get-ChildItem $dirRoot -Recurse -ErrorAction silentlycontinue) {
...
}
Run Code Online (Sandbox Code Playgroud)
小智 5
怎么样询问?
foreach($file in Get-ChildItem $dirRoot -Recurse -ErrorAction Inquire) {
...
}
Run Code Online (Sandbox Code Playgroud)
也许打开第二个PS窗口来解决错误,然后在第一个PS窗口中继续命令,选择Y继续.
您还可以使用ErrorVariable
foreach($file in Get-ChildItem $dirRoot -Recurse -ErrorVariable a) {
...
}
Run Code Online (Sandbox Code Playgroud)
Get-Variable a或$ a将显示该命令产生的所有错误.您还可以使用+ variablename(+ a)将错误添加到现有变量中.
foreach($file in Get-ChildItem $dirRoot -Recurse -ErrorVariable +a) {
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16055 次 |
最近记录: |