2012-02-16 89 views
2

我想在powershell中使用cleartool命令。第三方實用程序的異常處理不起作用

如果命令失敗,它應該捕獲異常並執行操作。但它並沒有被catch {}捕獲{}

try { 
     #If $viewname not exist it will throw error 
     cleartool lsview $ViewName 
    } 
catch { 

    # If list view fails , it means View doesn't exist. So create view 
    Write-host "Create view" 
    cleartool mkview -tag $ViewName -nsh $ccViews$ViewName".vws" 

} 

當try中的命令失敗時,它不會在catch中調用表達式。

是否catch命令不能與非.net相關的東西工作?

回答

2

我從來沒有見過cleartool的PowerShell腳本中使用的異常機制。
(我看到的這對夫婦在「how to find root[folder] for each component using cleartool?」和「How to describe recommend baseline with pipeline」)。

old thread (2006, so for the first version of Powershell)說明使用$?的錯誤管理機制:

cleartool lsco -cview -s . | 
foreach { 
    cleartool diff -pred -opt -sta "$_" 
    if ($?) { 
    cleartool unco -rm "$_" 
    } else { 
    cleartool ci -nc "$_" 
    } 
} 

要使用的機制,你可能要封裝你cleartool呼叫的調用命令,並從包裝函數返回一個狀態碼,如「catching return code of a command with 「invoke-command」 - Powershell 2」中所述。

或者,而不是調用直接cleartool,你可以嘗試調用CAL commands as in this script

+0

例子:http://cmdosndonts.blogspot.com/2010/11/enhancing-clearcase-with-powershell.html,但沒有抓住。 CAL API:http://stackoverflow.com/a/8550920/6309 – VonC 2012-02-16 09:07:49

2

由於cleartool是一個外部exe文件,它不會在PowerShell環境中引發異常。

根據IBM documentation:如果通過在交互模式下輸入quit命令退出cleartool,則退出狀態爲0.單命令模式下的退出狀態取決於命令是成功(零退出狀態)還是生成錯誤消息(非零退出狀態)。

在PowerShell中,您可以通過$LASTEXITCODE var獲得此非零退出狀態。正如@VonC所解釋的那樣,您可以使用$?來檢查單指令是否有效,然後用$LASTEXITCODE來得到特定的錯誤。 PowerShell腳本的