powershell - 逐个文件捕获“Get-ChildItem”错误
问题描述
我正在使用Get-ChildItem -Recurse
搜索目录。我不能保证所有Get-ChildItem
将被击中的东西都可以访问。我想记录这些失败,但不让整个Get-ChildItem -Recurse
命令失败。现在我有
Get-ChildItem -Recurse $targetdir -ErrorAction Inquire `
| where { $_.Name -eq $name } `
| foreach {
echo-indented "Found $(hash $_) at $($_.FullName)"
$_
}
有问题的代码是-ErrorAction Inquire
. 如果我这样做了-ErrorAction Stop
,我将不得不在某个地方放置一个try-catch。它必须围绕整个管道,对吗?在这种情况下,将不会找到并写出在不可访问项之后找到的子项。那我还能做什么?
解决方案
您可以使用-ErrorVariable
common 参数将错误保存到变量中。
Get-ChildItem -recurse foo -ErrorVariable err
$err
推荐阅读
- listview - 如何在 xamarin 表单的列表视图中使用 Web 视图
- c# - 为什么 IdentityServer 不认为 url 有效?
- php - ajax url 总是重定向到索引
- c - Do While 循环未执行且找不到错误(使用数组堆栈)
- c# - 正则表达式模式匹配多个组 C#
- java - @BeforeMethod/@AfterMethod(onlyForGroups) 方法没有执行,如果属于该组的测试方法被执行
- javascript - 为什么 removeChild() 在选择选项中不起作用
- apache - 使用 mod_rewrite 重定向循环
- javascript - 我的架构没有保存到我的数据库(MongoDB)
- javascript - 当 contenteditable 为 true 时,从一行复制文本并粘贴到另一列的“相邻”行