2013-07-05 29 views
2

如果我做了充分遞歸搜索磁盤的PowerShell和設置ErrorActionSilentlyContinue它仍然炸彈當它擊中一個文件夾不能訪問。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 

我會盡力的全局變量,我想有另一種方式。

+3

所寫的代碼對我來說工作正常......它是否顯示任何結果? – JNK

+1

無法重現它。請發佈確切的錯誤消息。當您設置'$ ErrorActionPreference =「默默繼續」'時,命令行爲是否有所不同?另外,請注意,即使這樣也不會捕捉到各種錯誤(請參閱[這裏](http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/08/hey-scripting-guy-march- 8-2010.aspx))。 –

+1

@Kevin_'ls'是'Get-ChildItem'的[別名](http://technet.microsoft.com/en-us/library/ee692685.aspx)。關於它沒什麼黑客。 –

回答

2

嘗試設置$ErrorActionPreference = "silentlycontinue"而不是使用參數-ErrorAction SilentlyContinue。有人在不久前向我提到-EA有時可能會起作用。

+0

嗯。事實證明,這在我的腳本中不起作用。它在命令行上工作,但是一旦它遇到無法訪問的文件夾,我的腳本仍會盡早返回。我感覺到一個連接錯誤。 –

+0

必須補充的是,腳本在構建服務器代理下運行時仍然不起作用,即使它在PS提示符下正常運行時也能正常工作。 –

0

因此Ansgar能夠沉默錯誤,但在腳本中錯誤有效地阻止了執行。

我沒有得到任何好處後,我心情不好地上牀睡覺。

今天早上我回到RDP到Windows Server 2008 R2虛擬機,並且看到它已經從Windows Update運行重新啓動。更神奇的是,這個問題已經神祕地消失了!

時機令人難以置信。這些是更新:

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。