Powershell ls-递归-错误操作SilentlyContinue不';行不通

Powershell ls-递归-错误操作SilentlyContinue不';行不通,powershell,Powershell,如果我使用ls命令对PowerShell中的磁盘执行完全递归搜索,并将ErrorAction设置为SilentlyContinue它在命中无法访问的文件夹时仍会爆炸 我可以将调用包装在一个try catch中,但这不会阻止搜索终止,它只会吞下错误 例如: ls -Include msdeploy.exe -Recurse -ErrorAction SilentlyContinue; 结果: PS C:\> ls msdeploy.exe -Recurse -ErrorAction Sil

如果我使用
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
有时可能会出现故障。

因此,Ansgar能够使错误静音,但在脚本中,错误实际上会自动停止执行

我在毫无进展后心情不好就上床睡觉了

今天早上,我返回RDP到WindowsServer2008R2虚拟机,发现它已从Windows更新运行中重新启动。更令人惊奇的是,这个问题已经神秘地消失了

时间安排令人难以置信。更新内容如下:

Product
-------
Security Update for Microsoft .NET Framework 4.5 on Windows 7, Vista, Windows Server 2008, Windows Server 2008 R2 for x64 (KB2840642)
Windows Malicious Software Removal Tool x64 - July 2013 (KB890830)
Security Update for Windows Server 2008 R2 x64 Edition (KB2835361)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB2840631)
Security Update for Windows Server 2008 R2 x64 Edition (KB2834886)
Security Update for Windows Server 2008 R2 x64 Edition (KB2850851)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB2833946)
Cumulative Security Update for Internet Explorer 10 for Windows Server 2008 R2 Service Pack 1 for x64-based Systems (KB2846071)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB2844286)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 for x64-based Systems (KB2832414)

但是,应该注意的是,这可能是重新启动(这是一台服务器)的行为,它可能已经替换了很久以前由于安装更新而挂起的DLL。

编写的代码对我来说很好……它是否显示了任何结果?也无法复制它。请发布准确的错误消息。设置
$ErrorActionPreference=“silentlycontinue”
时,命令的行为是否有所不同?另外,请注意,即使这样也不能捕获所有类型的错误(请参阅)。@Kevin
ls
是一个for
Get ChildItem
。“这并没有什么不妥的地方。”安斯加维切尔将此作为回答,它已经解决了。为什么,我不知道。事实证明,这在我的脚本中不起作用。它在命令行中工作,但我的脚本仍然会在它到达无法访问的文件夹时提前返回。我感觉到一个连接错误即将出现。必须补充的是,当脚本在build server代理下运行时,它仍然不起作用,即使它在PS提示符下“正常”运行时也起作用。