如果我使用该ls
命令在 PowerShell 中对磁盘进行完全递归搜索并将其设置ErrorAction
为SilentlyContinue
它仍然会在遇到无法访问的文件夹时爆炸。
我可以将调用包装在 a 中try catch
,但这不会阻止搜索终止,它只会吞下错误。
例子:
ls -Include msdeploy.exe -Recurse -ErrorAction SilentlyContinue;
结果是:
PS C:\> ls msdeploy.exe -Recurse -ErrorAction SilentlyContinue
Directory: C:\Program Files\IIS\Microsoft Web Deploy V2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/1/2011 8:17 PM 165704 msdeploy.exe
Directory: C:\Program Files\IIS\Microsoft Web Deploy V3
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/12/2012 9:13 AM 197504 msdeploy.exe
Directory: C:\Program Files (x86)\IIS\Microsoft Web Deploy V2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/1/2011 8:17 PM 169800 msdeploy.exe
Directory: C:\Program Files (x86)\IIS\Microsoft Web Deploy V3
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/12/2012 9:13 AM 201600 msdeploy.exe
ls : Access is denied
At line:1 char:1
+ ls msdeploy.exe -Recurse -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand
我会尝试全局变量,我认为还有另一种方法。