如果我做了充分遞歸搜索磁盤的PowerShell和設置ErrorAction
到SilentlyContinue
它仍然炸彈當它擊中一個文件夾不能訪問。LS -Recurse -ErrorAction SilentlyContinue不使用<code>ls</code>命令工作
我可以將呼叫包裝在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
我會盡力的全局變量,我想有另一種方式。
所寫的代碼對我來說工作正常......它是否顯示任何結果? – JNK
無法重現它。請發佈確切的錯誤消息。當您設置'$ ErrorActionPreference =「默默繼續」'時,命令行爲是否有所不同?另外,請注意,即使這樣也不會捕捉到各種錯誤(請參閱[這裏](http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/08/hey-scripting-guy-march- 8-2010.aspx))。 –
@Kevin_'ls'是'Get-ChildItem'的[別名](http://technet.microsoft.com/en-us/library/ee692685.aspx)。關於它沒什麼黑客。 –