Question

I am trying to use cleartool command in powershell.

If the command fails, it should catch the exception and do the action. But it was not caught by 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"

}

When commands in try fails , it doesn't invoke expressions in catch.

Whether catch command won't work with non .net related stuff?

Was it helpful?

Solution

I never saw the exception mechanism used in powershell script for cleartool.
(The couple I saw were in "how to find root[folder] for each component using cleartool?", and in "How to describe recommend baseline with pipeline").

This old thread (2006, so for the first version of Powershell) illustrates that error management mechanism using $?:

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

To use your mechanism, you might want to encapsulate your cleartool call in an Invoke-Command, and return a status code from the wrapper function, as described in "catching return code of a command with “invoke-command” - Powershell 2".

Or, instead of calling directly cleartool, you can try an call the CAL commands as in this script.

OTHER TIPS

As cleartool is an external exe file, it does'nt throw exception in PowerShell environnement.

According to IBM documentation : If you exit cleartool by entering a quit command in interactive mode, the exit status is 0. The exit status from single-command mode depends on whether the command succeeded (zero exit status) or generated an error message (nonzero exit status).

In PowerShell you can get this nonzero exit status with $LASTEXITCODE var. So as @VonC explain, you can use $? to check if the single-command works, and then $LASTEXITCODE to get the specific error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top