2013-06-27 73 views
1

我想這個捕捉異常,但它只是不相當只是工作顯示我的錯誤在我運行該腳本的腳本編輯器: -PowerShell的catch異常

路徑我在劇本中提到「\ server \ abc」並不存在,所以它應該將其作爲一個例外來捕獲它。 HELP HELP

Try 
{ 

Get-ChildItem -Path "\\server\abc" 

} 
Catch 
{ 

Write-Host "error" 

} 

回答

8

您需要設置erroraction停止爲被終止的錯誤 - 只終止錯誤被升高到catch塊。

Try 
{ 

Get-ChildItem -Path "\\server\abc" -ErrorAction Stop 

} 
Catch 
{ 

Write-Host "error" 

} 
+0

使用$ ErrorActionPreference = 「停止」 更多參考:http://blogs.msdn.com/b/kebab/archive/2013/06/09/an-introduction-to-error-handling-in- powershell.aspx – Amitabha