2

如果我使用该ls命令在 PowerShell 中对磁盘进行完全递归搜索并将其设置ErrorActionSilentlyContinue它仍然会在遇到无法访问的文件夹时爆炸。

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

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

4

2 回答 2

2

尝试设置$ErrorActionPreference = "silentlycontinue"而不是使用参数-ErrorAction SilentlyContinue。前段时间有人向我提到过,-EA有时可能会采取行动。

于 2013-07-05T13:37:59.027 回答
0

因此 Ansgar 能够使错误静音,但在脚本中,错误有效地静默停止了执行。

在无处可去之后,我心情不好地去睡觉了。

今天早上我回到 RDP 到 Windows Server 2008 R2 虚拟机,发现它已经从 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。

于 2013-07-11T08:33:04.740 回答