gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:47:09 31 4
gpt4 key购买 nike

如果我使用ls命令在PowerShell中对磁盘进行完全递归搜索,并将ErrorAction设置为SilentlyContinue,当它命中无法访问的文件夹时,它仍会爆炸。

我可以将 call 包装在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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com