powershell - ls -Recurse -ErrorAction SilentlyContinue 不起作用

标签 powershell

如果我使用 ls 命令在 PowerShell 中对磁盘进行完全递归搜索,并将 ErrorAction 设置为 SilentlyContinue,它仍然会在以下情况下发生爆炸:它到达了无法访问的文件夹。

我可以将调用包装在 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

我会尝试全局变量,我认为还有另一种方法。

最佳答案

尝试设置$ErrorActionPreference = "silentlycontinue",而不是使用参数-ErrorAction SilentlyContinue。不久前有人向我提到-EA有时可能会出问题。

关于powershell - ls -Recurse -ErrorAction SilentlyContinue 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17489372/

相关文章:

Powershell 脚本命令持久化

c# - 如何使用 C# 将 powershell 脚本编码为 base64 UTF16-LE 字符串

powershell - 如何从单独的 ps1 文件访问自定义 PowerShell 5.0 类

powershell - 如何检索原始的、未解析(未扩展)的注册表值?

iis - appcmd.exe 添加/更改网站子目录的 MIME 类型

c# - 从 C# 主机运行 Get-Service

具有多个 session 的 Powershell 独立脚本

powershell - Windows Powershell 是否有 Try/Catch 或其他错误处理机制?

powershell - F5 BIG-IP Powershell/iControl - 查询节点级别的当前连接

powershell - 切换两个文件的名称