2013-05-08 86 views
0

嗨,我試圖找出如何做到這一點或做的另一種方式,它PowerShell的試用和攔截執行多個命令中嘗試

try { 
Get-ADComputer -Identity namedoesnotexist 
(Get-ChildItem).FullName 
} 
catch {$_ | Out-File log.log} 

當運行這段代碼,我使用的是不存在的,所以我得到一個名字一個錯誤和catch會把它寫到我的日誌文件中(只是一個例子) 我想完成的是錯誤被捕獲,但try語句繼續運行我的Get-Childitem命令並嘗試這一點。 任何其他簡單的方法呢?

回答

1

將只有一行在在try..catch會給你的效果

try 
{ 
    Get-ADComputer -Identity namedoesnotexist 
} 
catch 
{ 
    $_ | Out-File log.log 
} 
(Get-ChildItem).FullName 

但也許trap是你在找什麼

trap 
{ 
    $_ | Out-File log.log 
    continue # remove this if you still want to see each error 
} 
Get-ADComputer -Identity namedoesnotexist 
(Get-ChildItem).FullName 
+1

使用'-Append'開關' Out-File「,因此您不會在每個錯誤上覆蓋日誌文件。 – 2013-05-08 20:04:16

+0

陷阱正是我正在尋找的。謝謝! – TelefoneN 2013-05-10 08:44:01